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
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...
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.
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));
If you have Boost, then it has the simplest way. Have a look at to_upper()/to_lower() in Boost string algorithms.