Convert a unicode String In C++ To Upper Case

后端 未结 9 2095
予麋鹿
予麋鹿 2020-12-01 14:51

How we can convert a multi language string or unicode string to upper/lower case in C or C++.

9条回答
  •  醉梦人生
    2020-12-01 15:09

    If your system is already in UTF-8, by using std::use_facet, you can write:

    #include 
    #include 
    
    int main() {
        std::locale::global(std::locale(""));  // (*)
        std::wcout.imbue(std::locale());
        auto& f = std::use_facet>(std::locale());
    
        std::wstring str = L"Zoë Saldaña played in La maldición del padre Cardona.";
    
        f.toupper(&str[0], &str[0] + str.size());
        std::wcout << str << std::endl;
    
        return 0;
    }
    

    And you get (http://ideone.com/AFHoHC):

    ZOË SALDAÑA PLAYED IN LA MALDICIÓN DEL PADRE CARDONA.

    If it don't work you will have to change (*) into std::locale::global(std::locale("en_US.UTF8")); or an UTF-8 locale you actually have on the plateform.

提交回复
热议问题