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

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sat Dec 11 08:05:01 UTC 2021


On Saturday, 11 December 2021 at 00:39:15 UTC, forkit wrote:
> On Friday, 10 December 2021 at 22:35:58 UTC, Arjan wrote:
>>
>> "abc;def;ghi".tr(";", "", "d" );
>>
>
> I don't think we have enough ways of doing the same thing yet...
>
> so here's one more..
>
> "abc;def;ghi".substitute(";", "");

Using libraries can trigger hidden allocations.

```
import std.stdio;

string garbagefountain(string s){
     if (s.length == 1) return s == ";" ? "" : s;
     return garbagefountain(s[0..$/2]) ~ 
garbagefountain(s[$/2..$]);
}

int main() {
     writeln(garbagefountain("abc;def;ab"));
     return 0;
}

```



More information about the Digitalmars-d-learn mailing list