【问题标题】:How to concatenate 2 LPOLESTR如何连接 2 个 LPOLESTR
【发布时间】:2011-02-24 05:53:58
【问题描述】:

我想在 C++ 中连接 2 个字符串,我不能使用 char*。

我尝试了以下方法但不起作用:

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

我需要一个 s1 和 s2 连接的字符串。有任何信息或网站可以对此进行更多解释吗?谢谢。

【问题讨论】:

标签: c++ string bho concatenation wstring


【解决方案1】:

OLESTR("s")L"s" 一样(而OLESTR(s)Ls),这显然不是你想要的。
使用这个:

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

这会给你一个LPCOLESTR(即const LPOLESTR)。如果你真的需要它是非常量的,你可以将它复制到一个新的字符串:

...
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!

或者,完全避免使用 wstring(不推荐;最好将您的应用程序转换为在任何地方使用 wstring's,而不是使用 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!

【讨论】:

    【解决方案2】:

    您缺少 L 来完成 s2 的作业。

    wstring s2 = L"/page.html";
    

    【讨论】:

    • 刚刚更改了它,但它给了我:'错误 C2065:'Ls': undeclared identifier' on line 'LPOLESTR o = OLESTR(s);'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2018-12-22
    • 2015-10-23
    相关资源
    最近更新 更多