开发者

How to concatenate 2 LPOLESTR

i want to concatenate 2 strings in c++, i can't use char*.

I tried the following but doesn't work:

#define url L"http://domain.com"
wstring s1 = url;
wstring s2 = L"/page.html";
wstring s = s1 + s2;
LPOLESTR o = OLESTR(s);
开发者_运维问答

I need a string with s1 and s2 concatenated. Any info or website that explain more about this ? Thanks.


OLESTR("s") is the same as L"s" (and OLESTR(s) is Ls), which is obviously not what you want.
Use this:

#define url L"http://domain.com"
wstring s1 = url;
wstring s2 = L"/page.html";
wstring s = s1 + s2;
LPCOLESTR o = s.c_str();

This gives you a LPCOLESTR (ie. a const LPOLESTR). If you really need it to be non-const, you can copy it to a new string:

...
wstring s = s1 + s2;
LPOLESTR o = new wchar_t[s.length() + 1];
wcscpy(o, s.c_str()); //wide-string equivalent of strcpy is wcscpy
//Don't forget to delete o!

Or, to avoid wstring's altogether (not recommended; it would be better to convert your application to using wstring's everywhere than to use LPOLESTR's):

#define url L"http://domain.com"
LPCOLESTR s1 = url;
LPCOLESTR s2 = L"/page.html";
LPOLESTR s = new wchar_t[wcslen(s1) + wcslen(s2) + 1];
wcscpy(s, s1); //wide-string equivalent of strcpy is wcscpy
wcscat(s, s2); //wide-string equivalent of strcat is wcscat
//Don't forget to delete s!


You're missing the L to make s2's assignment work.

wstring s2 = L"/page.html";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜