Convert a unicode String In C++ To Upper Case

后端 未结 9 2114
予麋鹿
予麋鹿 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

    Set the locale first, example :

    setlocale(LC_ALL, "German")); /*This won't work as per comments below */
    
    setlocale(LC_ALL, "English"));
    
    setlocale( LC_MONETARY, "French" );
    
    setlocale( LC_ALL, "" ); //default locale 
    

    Then use

    std::use_facet std::locale as follows:-

    typedef std::string::value_type char_t;
    char_t upcase( char_t ch )
    {
     return std::use_facet< std::ctype< char_t > >( std::locale() ).toupper( ch );
    }
    
    std::string toupper( const std::string &src )
    {
     std::string result;
     std::transform( src.begin(), src.end(), std::back_inserter( result ), upcase );
     return result;
    }
    
    const std::string src  = "Hello World!";
    std::cout << toupper( src );
    

提交回复
热议问题