LPWSTR字符串的连接

Concatenation of LPWSTR strings

本文关键字:连接 字符串 LPWSTR      更新时间:2023-10-16

在Visual C++中,我有一个

LPWSTR mystring;

这已经在代码中的其他地方定义了。

我想创建一个新的LPWSTR,其中包含:

"hello " + mystring + " blablabla"        (i.e. a concatenation)

这么简单的事情(串联(让我很生气!提前谢谢你,我迷路了!

C++方式:

std::wstring mywstring(mystring);
std::wstring concatted_stdstr = L"hello " + mywstring + L" blah";
LPCWSTR concatted = concatted_stdstr.c_str();

您可以使用StringCchCatW函数

std::wstring mystring_w(mystring);
std::wstring out_w = L"hello " + mystring_w + L" blablabla";
LPWSTR out = const_cast<LPWSTR>(out_w.c_str());

"out"是"out_w"的LPWSTR包装。因此,只要"out_w"在作用域中,就可以使用它。此外,您不需要删除"out",因为它绑定到"out_w"生命周期。

这与"user529758"给出的答案大致相同,但"chris"提出了修改意见。