foreach over string

Etienne Cimon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 24 09:57:57 PDT 2014


On 2014-05-24 12:46, Kagamin wrote:
> foreach over string apparently iterates over chars by default instead of
> dchars. Didn't it prefer dchars?
>
> string s="weiß";
> int i;
> foreach(c;s)i++;
> assert(i==5);


A string is defined by:
alias string = immutable(char)[];

It doesn't add anything to that type (unless you import a library like 
std.algorithm, which adds many "methods" thanks to UFCS and generic 
functions)

I believe you are looking for dstring which is defined by:
alias dstring = immutable(dchar)[];

dstring s="weiß";
int i;
foreach(c;s)i++;
assert(i==4);


More information about the Digitalmars-d-learn mailing list