how to convert a string (wstring) from lowercase to uppercase characters and vice versa?
I searched the net and found there is a STL-function std::transform.
But until now I hav'nt figured out how to give the right locale-object for example "Germany_german" to the function.
Who can help please?
my code looks like:
wstring strin = L"ABCÄÖÜabcäöü";
wstring str = strin;
locale loc( "Germany_german" ); // ??? how to apply this ???
std::transform( str.begin(), str.end(), str.begin(), (int(*)(int)tolower );
//result: "abcäöüabcäöü"
The characters ÄÖÜ and äöü (it's like Ae, Oe, Ue) will not be converted correctly.
P.S.: I don't prefer a big switch sweat and also I know BOOST is capable of everything, i would prefer a STL solution.
thanks in advance
Oops
You need to apply it in different way:
for(unsigned i=0;i<str.size();i++)
str[i]=std::toupper(str[i],loc);
Or set global locale
std::locale::global(std::locale("Germany_german"));
and then
std::transform( str.begin(), str.end(), str.begin(), std::toupper );
See: http://www.cplusplus.com/reference/std/locale/tolower/
Note 1 C toupper is different from std::toupper which receives std::locale as parameter it uses char
only as parameter while std::toupper
works on both char
and wchar_t
.
Note 2 std::locale
is quite broken for German because it works on character basis and can't convert "ß" to "SS" but it would work for most of other characters...
Note 3: If you need correct case conversion including handling characters like "ß", you need use good localization library like ICU, or Boost.Locale
The problem is that tolower()
/toupper()
are fundamentally broken for many locales. How do you propose toupper()
should work with words containing an 'ß'
? What would be the all-upper-case of "Maße"
?[1] There are similar examples in French and other languages.
So the first thing to ask yourself is why you need this and what you want to achieve.
If you're willing to ignore all these problems, here is the first treatment on the topic google found me. (If you're going to google for more of this, James Kanze has over the years posted many many useful insights into this problem in quite a few of the C++ newsgroups.)
[1] A hint for the few non-Germans hanging out here :)
: In German, the "ß", literally a ligature of "sz", is not available as a capital letter. It can be replaced by "ss", but not always. For example, "Masse" is a completely different word than "Maße".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With