Insert a char in string

Alexandre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 09:56:59 PDT 2014


Oh, I used that letters in upper case, just for a simple sample...

On Thursday, 10 July 2014 at 16:32:53 UTC, Marc Schütz wrote:
> On Thursday, 10 July 2014 at 16:20:29 UTC, Alexandre wrote:
>> Sorry..
>> I mean:
>>
>> auto X = "100000000000000";
>> auto N = X.insertInPlace(3,',');
>>
>> On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote:
>>> I have a string X and I need to insert a char in that 
>>> string...
>>>
>>> auto X = "100000000000000";
>>>
>>> And I need to inser a ',' in position 3 of this string..., I 
>>> try to use the array.insertInPlace, but, not work...
>>>
>>> I try this:
>>> auto X = "100000000000000";
>>> auto N = X.insertInPlace(1,'0');
>
> `std.array.insertInPlace` doesn't return anything. "In place" 
> here means "in situ", i.e. it will not create a new string, but 
> insert the new elements into the existing one. This operation 
> may still reallocate, in which case the array slice you're 
> passing in will be updated to point to the new memory.
>
> Either use this instead:
>
>     auto x = "100000000000000";
>     auto n = x.dup;
>     n.insertInPlace(3, ',');
>     // or: insertInPlace(n, 3, ',');
>
> ... or use slicing and concatenating to construct a new string:
>
>     auto g = x[0 .. 3] ~ ',' ~ x[3 .. $];
>
> (Side note about style: It's common practice to use lower-case 
> names for variables, upper-case first letters are used to 
> denote types. But of course, that's a matter of taste.)



More information about the Digitalmars-d-learn mailing list