Remove function?

Brad Anderson eco at gnuk.net
Wed Dec 4 18:06:58 PST 2013


On Wednesday, 4 December 2013 at 22:13:52 UTC, seany wrote:
> does all the algorithms defined in std.string and std.algorithm 
> also apply on char[] ?

Pretty much. If one doesn't it'd be a considered a bug. front() 
and popFront() defined for arrays in std.array specialize for 
narrow strings (that is, UTF-8 and UTF-16 encoded strings whether 
they are immutable(char)[] or just char[]) so they evaluate by 
code point.  This means all algorithms that make use of ranges 
automatically work for either case.

> is there any way to force a string to become mutable?

You could just bash it over the head with a cast() but you should 
not do that.  You can call .dup() to return a mutable copy of a 
string.  I'm not actually sure how well remove() works with 
narrow strings though. Seems like it doesn't seem to like them in 
my quick test.  Non-narrow strings work fine though.

     // http://dpaste.dzfl.pl/0a269245
     void main()
     {
         import std.algorithm, std.stdio;

         dstring immutable_str = "this is an immutable string"d;
         dchar[] mutable_str = immutable_str.dup;
         mutable_str = mutable_str.remove(9, 11, 12);
         writeln(immutable_str);
         writeln(mutable_str);
     }


More information about the Digitalmars-d-learn mailing list