C++ 获取std::wstring的子字符串
如何获取包含一些非ASCII字符的C++ 获取std::wstring的子字符串,c++,substring,non-ascii-characters,wstring,C++,Substring,Non Ascii Characters,Wstring,如何获取包含一些非ASCII字符的std::wstring的子字符串 以下代码不输出任何内容: (文本是一个阿拉伯语单词,包含4个字符,每个字符有两个字节,加上单词“Hello”) #包括 #包括 使用名称空间std; int main() { wstring s=L“你好”; wcout这应该有效: #include <iostream> #include <string> #include <boost/regex/pending/unicode_iterato
std::wstring
的子字符串
以下代码不输出任何内容:(文本是一个阿拉伯语单词,包含4个字符,每个字符有两个字节,加上单词“Hello”)
#包括
#包括
使用名称空间std;
int main()
{
wstring s=L“你好”;
wcout这应该有效:
#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;
}
坦率地说不过,我认为你的子字符串
调用做出了奇怪的假设。让我马上提出一个解决方案:第二个至少应该打印“地狱”,并在Coliru上运行。第一个可能无法在您预期使用的控制台上打印。是的,这是奇怪的部分。我什么也没有得到。您在什么操作系统上运行此代码?恐怕控制台对Unicode的支持有限(由于CRT怪异和控制台可用字体选择的限制),但YMMV。特别是,首先要正确设置整个内容,以便在控制台上打印Unicode(请参阅和,如果默认字体没有您需要的字形),然后使用子字符串和其他任何东西进行实验。您对“一分钟”的定义是7年吗?@codedogo显然我偶尔会忘记一些事情:)
#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