String To Lower/Upper in C++

前端 未结 10 1938
挽巷
挽巷 2020-12-08 20:10

What is the best way people have found to do String to Lower case / Upper case in C++?

The issue is complicated by the fact that C++ isn\'t an English only programmi

相关标签:
10条回答
  • 2020-12-08 20:41

    I have found a way to convert the case of unicode (and multilingual) characters, but you need to know/find (somehow) the locale of the character:

    #include <locale.h>
    
    _locale_t locale = _create_locale(LC_CTYPE, "Greek");
    AfxMessageBox((CString)""+(TCHAR)_totupper_l(_T('α'), locale));
    _free_locale(locale);
    

    I haven't found a way to do that yet... I someone knows how, let me know.

    Setting locale to NULL doesn't work...

    0 讨论(0)
  • 2020-12-08 20:42

    What Steve says is right, but I guess that if your code had to support several languages, you could have a factory method that encapsulates a set of methods that do the relevant toUpper or toLower based on that language.

    0 讨论(0)
  • 2020-12-08 20:44

    For copy-pasters hoping to use Nic Strong's answer, note the spelling error in "use_factet" and the missing third parameter to std::transform:

    locale loc("");
    const ctype<char>& ct = use_factet<ctype<char> >(loc);
    transform(str.begin(), str.end(), std::bind1st(std::mem_fun(&ctype<char>::tolower), &ct));
    

    should be

    locale loc("");
    const ctype<char>& ct = use_facet<ctype<char> >(loc);
    transform(str.begin(), str.end(), str.begin(), std::bind1st(std::mem_fun(&ctype<char>::tolower), &ct));
    
    0 讨论(0)
  • 2020-12-08 20:52

    If you have Boost, then it has the simplest way. Have a look at to_upper()/to_lower() in Boost string algorithms.

    0 讨论(0)
提交回复
热议问题