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

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sun Dec 12 08:58:29 UTC 2021


On Saturday, 11 December 2021 at 19:50:55 UTC, russhy wrote:
> you need to import a 8k lines of code module that itself 
> imports other modules, and then the code is hard to read

I agree.

```
@safe:

auto deatheater(char stripchar)(string str) {
	struct voldemort {
         immutable(char)* begin, end;
         bool empty(){ return begin == end; }
         char front(){ return *begin; }
         char back()@trusted{ return *(end-1); }
         void popFront()@trusted{
             while(begin != end){begin++; if (*begin != stripchar) 
break; }
         }
         void popBack()@trusted{
             while(begin != end){end--; if (*(end-1) != stripchar) 
break; }
         }
         this(string s)@trusted{
             begin = s.ptr;
             end = s.ptr + s.length;
         }
	}
     return voldemort(str);
}


void main() {
     import std.stdio;
     string str = "abc;def;ab";
     foreach(c; deatheater!';'(str)) write(c);
     writeln();
     foreach_reverse(c; deatheater!';'(str)) write(c);
}

```




More information about the Digitalmars-d-learn mailing list