如何连接字符串和wstring
How do I concatenate a string and wstring?
我在C++中有一个wstring变量和一个字符串变量。我想将两者连接起来,但简单地将它们添加在一起会产生构建错误。我如何将两者结合起来?如果我需要将wstring变量转换为字符串,我将如何实现这一点?
//A WCHAR array is created by obtaining the directory of a folder - This is part of my C++ project
WCHAR path[MAX_PATH + 1] = { 0 };
GetModuleFileNameW(NULL, path, MAX_PATH);
PathCchRemoveFileSpec(path, MAX_PATH);
//The resulting array is converted to a wstring
std::wstring wStr(path);
//An ordinary string is created
std::string str = "Test";
//The variables are put into this equation for concatenation - It produces a build error
std::string result = wStr + str;
首先将wstring
转换为string
,如下所示:
std::string result = std::string(wStr.begin(), wStr.end()) + str;
或者如果wStr
包含非ASCII字符:
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::string wStrAsStr = converter.to_bytes(wStr);
std::string result = wStrAsStr + str;
相关文章:
- 在 C++ 中将 utf8 wstring 转换为窗口中的字符串
- 使用 ReadProcessMemory() 将未知大小的字符数组或字符串读入 wstring 中
- 提升程序参数可以解析WSTRING,但将其迫使其施加到字符串中
- 从相同的硬编码字符串文字初始化std::string和std::wstring
- 转换一个带有UTF8 WSTRING控制台的装饰的字符串
- 如何将平台^字符串转换为wstring,然后与L字符串连接
- C++ 字符串 - 如何为 wstring 实现此'IsEmptyOrSpace(string)'方法?
- 在窗口上将格式化的 C 字符串和参数转换为 wstring
- 字符串到Wstring C++Visual Studio
- C++:比较字符串或包含特殊字符(á、é、ő 等)的字符串或 wstring )
- 使用字符串构造函数从wstring转换为字符串时,是否没有可能的数据丢失
- C++错误(将 wstring 转换为字符串):无法将参数 1 从 std::wchar_t 转换为 std::char
- 如何将uint16_t转换为宽字符串(std::wstring)
- 字符集在字符串和 wstring 中的存储方式
- C++将字符串转换为 wstring 并使用最小代码返回
- 将字符串转换为wstring[不带区域设置的俄语符号::global]
- 火鸟在“UTF8 字符串转换为 wstring”时崩溃
- 获取std::wstring的子字符串
- 如何连接字符串和wstring
- 如何将C++dll中的unicode字符串/wstring/CStringW返回到InstallScript