Insert a char in string

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 12 17:02:44 PDT 2014


On 07/10/2014 09:05 AM, 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');

Here is another solution, which does not modify the original string:

import std.stdio;
import std.algorithm;
import std.range;
import std.string;

void main()
{
     auto number = "12345678";

     auto formatted =
         zip(number.retro, sequence!"n + 1")
         .map!(z => format(!(z[1] % 3) ? "%s," : "%s", z[0]))
         .join
         .retro;

     assert(formatted.equal("12,345,678"));
}

I am not happy with the .join there because I think it makes an array 
but I could not get it to compile with the lazy .joiner because I think 
it dose not look like a bidirectional range to .retro.

Ali



More information about the Digitalmars-d-learn mailing list