string concatenation

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun Apr 8 02:25:40 PDT 2012


On 4/8/12, dnewbie <run3 at myopera.com> wrote:
> I have a wchar[] and I want to convert it to UTF8
> then append a string. This is my code.
>
> import std.c.windows.windows;
> import std.string;
> import std.utf;
>
> int main()
> {
>    wchar[100] v;
>    v[0] = 'H';
>    v[1] = 'e';
>    v[2] = 'l';
>    v[3] = 'l';
>    v[4] = 'o';
>    v[5] = 0;
>    string s = toUTF8(v) ~ ", world!";
>    MessageBoxA(null, s.toStringz, "myapp", MB_OK);
>    return 0;
> }

You're using a wide string, then converting it to UTF8, and then
converting that to a zero-terminated string for a function that
expects ASCII.

Just use this:
string s = "Hello world!";
MessageBoxA(null, s.toStringz, "myapp", MB_OK);


More information about the Digitalmars-d-learn mailing list