2013-08-19 5 views
2

일부 ASCII 문자가 아닌 std::wstring의 하위 문자열을 어떻게 구할 수 있습니까?
(텍스트는 아랍어 단어가 각 문자가 2 바이트를 가지고 4 문자가 포함되어 있습니다, 플러스 단어 "안녕하세요")std :: wstring의 하위 문자열을 가져 오는 중

#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; 
} 
+0

두 번째는 적어도 "지옥"을 인쇄해야하며 Coliru는 수행해야합니다. 첫 번째 버전은 현재 사용하고있는 콘솔에 인쇄 할 수 없습니다. – chris

+0

예, 그건 이상한 부분입니다. 나는 아무것도 얻지 못하고있다. – MBZ

+0

이 코드를 실행중인 OS는 무엇입니까? –

답변

0

이 작동합니다 :

다음 코드는 아무것도 출력하지 않습니다 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; 
} 

인쇄

سلا 
hell 

솔직히 그래도 내 생각에 substring 전화가 이상한 가정을하고 있다고 생각합니다. 잠시 후 그 문제를 해결하도록 제안 해주세요 :

+0

Windows에서 콘솔 출력을 위해 UTF8을 시도해보십시오. –

관련 문제