获取std::wstring的子字符串
getting a sub string of a std::wstring
如何获取包含一些非ASCII字符的std::wstring
的子字符串?
以下代码不输出任何内容:
(文本是一个阿拉伯语单词,包含4个字符,每个字符有两个字节,加上单词"Hello")
#include <iostream>
#include <string>
using namespace std;
int main()
{
wstring s = L"سلام hello";
wcout << s.substr(0,3) << endl;
wcout << s.substr(4,5) << endl;
return 0;
}
这应该有效:在Coliru上生存
#include <iostream>
#include <string>
#include <boost/regex/pending/unicode_iterator.hpp>
using namespace std;
template <typename C>
std::string to_utf8(C const& in)
{
std::string result;
auto out = std::back_inserter(result);
auto utf8out = boost::utf8_output_iterator<decltype(out)>(out);
std::copy(begin(in), end(in), utf8out);
return result;
}
int main()
{
wstring s = L"سلام hello";
auto first = s.substr(0,3);
auto second = s.substr(4,5);
cout << to_utf8(first) << endl;
cout << to_utf8(second) << endl;
}
打印
سلا
hell
坦率地说不过,我认为您的substring
呼叫做出了奇怪的假设让我在一分钟内提出解决方案:
相关文章:
- 在 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