iteration over a string

Ali Çehreli acehreli at yahoo.com
Tue May 28 00:42:46 PDT 2013


On 05/28/2013 12:26 AM, Timothee Cour wrote:

 > In all that follows, I don't want to go through intermediate UTF32
 > representation by making a copy of my string, but I want to iterate over
 > its code points.

Yes, the whole situation is a little messy. :)

There is also std.range.stride:

     foreach (ai; a.stride(1)) {
         // ...
     }

If you need the index as well, and do not want to manage it explicitly, 
one way is to use zip and sequence:

import std.stdio;
import std.range;

void main()
{
     string a="Ωabc";

     foreach (i, ai; zip(sequence!"n", a.stride(1))) {
         write(i,",",ai," ");
     }
}

The output:

Ω a b c

Ali



More information about the Digitalmars-d-learn mailing list