我想在 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 连接的字符串。有任何信息或网站可以对此进行更多解释吗?谢谢。
最佳答案
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
,而不是使用 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!
关于c++ - 如何连接 2 个 LPOLESTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2914202/