TDPL: Foreach over Unicode string

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Jul 27 15:07:40 PDT 2010


On page 123 there's an example of what happens when traversing a unicode string with a char, and on the next page the string is traversed with a dchar, which should fix the output. But I'm getting different results, here's the code and output of the two samples:

import std.stdio;

void main() {
    string str = "Hall\u00E5, V\u00E4rld!";
    foreach (c; str) {
        write('[', c, ']');
    }
    writeln();
}

Prints:
[H][a][l][l][Ã][¥][,][ ][V][Ã][¤][r][l][d][!]

Second example:

import std.stdio;

void main() {
    string str = "Hall\u00E5, V\u00E4rld!";
    foreach (dchar c; str) {
        write('[', c, ']');
    }
    writeln();
}

Prints:
[H][a][l][l][å][,][ ][V][ä][r][l][d][!]


The second example should print out:
[H][a][l][l][å][,][ ][V][ä][r][l][d][!] 

This is on DMD 2.047 on Windows.


More information about the Digitalmars-d mailing list