getting a sub string of a std::wstring

守給你的承諾、 提交于 2019-12-06 09:43:48

This should work: live on 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;
}

Prints

سلا
 hell

Frankly though, I think your substring calls are making weird assumptions. Let me suggest a fix for that in a minute:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!