How to loop through characters of a string in D language?
forkit
forkit at gmail.com
Wed Dec 8 22:18:23 UTC 2021
On Wednesday, 8 December 2021 at 14:27:22 UTC, BoQsc wrote:
> On Wednesday, 8 December 2021 at 14:16:16 UTC, bauss wrote:
>> 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
>>> ```
>>
>> string b = a.replace(";", "");
>
> Thanks, that's what I used to do few years ago.
> It's a great solution I forget about and it works.
>
> ```
> import std.stdio;
> import std.array;
>
> void main()
> {
> string a="abc;def;ab";
> string b = a.replace(";", "");
> writeln(b);
> }
> ```
It's also worth noting the differences in compiler output, as
well as the time taken to compile, these two approaches:
(1)
string str = "abc;def;ab".filter!(c => c != ';').to!string;
(2)
string str = "abc;def;ab".replace(";", "");
see: https://d.godbolt.org/z/3dWYsEGsr
More information about the Digitalmars-d-learn
mailing list