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

Rumbu rumbu at rumbu.ro
Fri Dec 10 12:15:18 UTC 2021


On Friday, 10 December 2021 at 11:06:21 UTC, IGotD- wrote:
> On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote:
>
>>
>> Since it seems there is a contest here:
>>
>> ```d
>> "abc;def;ghi".split(';').join();
>> ```
>>
>> :)
>
> Would that become two for loops or not?

I thought it's a beauty contest.

```d
string stripsemicolons(string s)
{
   string result;
   // prevent reallocations
   result.length = s.length;
   result.length = 0;

   //append to string only when needed
   size_t i = 0;
   while (i < s.length)
   {
     size_t j = i;
     while (i < s.length && s[i] != ';')
       ++i;
     result ~= s[j..i];
   }
}
```



More information about the Digitalmars-d-learn mailing list