How to loop through characters of a string in D language?
Ola Fosheim Grøstad
ola.fosheim.grostad at gmail.com
Fri Dec 10 23:53:47 UTC 2021
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov
wrote:
> Oooh, finally someone suggested to preallocate storage for all
> these reinventions of the wheel :D
```
import std.stdio;
char[] dontdothis(string s, int i=0, int skip=0){
if (s.length == i) return new char[](i - skip);
if (s[i] == ';') return dontdothis(s, i+1, skip+1);
auto r = dontdothis(s, i+1, skip);
r[i-skip] = s[i];
return r;
}
int main() {
string s = "abc;def;ab";
string s_new = cast(string)dontdothis(s);
writeln(s_new);
return 0;
}
```
More information about the Digitalmars-d-learn
mailing list