www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - string concatenation

reply dnewbie <run3 myopera.com> writes:
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;
}

I want "Hello, world!", but the result is "Hello" only. Please help me.
Apr 07 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
dnewbie:

    string s = toUTF8(v) ~ ", world!";
    MessageBoxA(null, s.toStringz, "myapp", MB_OK);
    return 0;
 }

I suggest to compile all your code with -property plus -w (or -wi), unless you have some specific needs to not do it. Bye, bearophile
Apr 08 2012
prev sibling next sibling parent "Stefan" <stefan schuerger.com> writes:
On Sunday, 8 April 2012 at 05:08:15 UTC, dnewbie wrote:
    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);

Hint: You normally don't use fixed-length arrays in D - unless there is a model world restriction, such as in class Car{ Tire[4] tires; ... } So if you really want to have UTF16 conversion and back (which I guess you don't), your code would be: wchar[] v = "Hello"w; string s = toUTF8(v) ~ ", world!"; MessageBoxA(null, s.toStringz, "myapp", MB_OK); What you probably want is string v = "Hello"; string s = v ~ ", world!"; MessageBoxA(null, s.toStringz, "myapp", MB_OK); Cheers, Stefan
Apr 08 2012
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 08 Apr 2012 01:08:09 -0400, dnewbie <run3 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;

D does not use null terminated strings, so...
    string s = toUTF8(v) ~ ", world!";

a fixed-sized wchar array is always passed full-bore. What you are doing is appending ", world!" to a 100-element char array. The resulting string is: Hello\0\xff\xff\xff....\xff\xff, world where that xff represnts the octet 0xff as a char, to fill out the 100 elements. So what you want is a slice of the original string, use v[0..n] where n is the length of the string. Since you don't need that 0, you can just do v[0..5]: string s = toUTF8(v[0..5]) ~ ", world!";
    MessageBoxA(null, s.toStringz, "myapp", MB_OK);
    return 0;
 }

 I want "Hello, world!", but the result is "Hello" only. Please help me.

Yeah, that's what I would have expected. MessageBox is hitting that 0 embedded in the string and stopping. -Steve
Apr 09 2012