How to loop through characters of a string in D language?

Salih Dincer salihdb at hotmail.com
Thu Dec 9 00:20:08 UTC 2021


On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:
> Let's say I want to skip characters and build a new string.
>
> The string example to loop/iterate:
>
> ```
> import std.stdio;
>
> void main()
> {
>     string a="abc;def;ab";
>
> }
> ```
>
> The character I want to skip: `;`
>
> Expected result:
> ```
> abcdefab
> ```
I always use split() and joiner pair. You can customize it as you 
want:
```d
import std.stdio : writeln;
import std.algorithm : joiner;
import std.array : split;

bool isWhite(dchar c) @safe pure nothrow @nogc
{
   return c == ' ' || c == ';' ||
         (c >= 0x09&& c <= 0x0D);
}

void main()
{
     string str = "a\nb   c\t;d e f;a  b ";
     str.split!isWhite.joiner.writeln(); //abcdefab
}
```


More information about the Digitalmars-d-learn mailing list