I want to convert wstring
into lower case. I found that there are a lot of answer using locale info. Is there any function like ToLower()
for wstring
also?
Asked
Active
Viewed 2.2k times
8

Dovydas Šopa
- 2,282
- 8
- 26
- 34

msing
- 81
- 1
- 1
- 2
-
2http://en.cppreference.com/w/cpp/string/wide/towlower – πάντα ῥεῖ Feb 03 '17 at 10:40
-
Note that "lower case" is an inherently locale-specific operation. In Turkey, `towlower('I'L) != 'i'L` (the result is actually `'ı'`) – Martin Bonner supports Monica Feb 03 '17 at 11:32
-
1In fact, case transformations are a nightmare. As noted in the link, lowercase 'Σ' is either 'σ' and 'ς' depending on the position in the word, and lowercase "SS" in German is either "ß" or "ss" - depending on the word ("MASSE" can either be "Maße" or "masse" depending on which homograph it is!). See http://unicode.org/faq/casemap_charprop.html for more gory details. – Martin Bonner supports Monica Feb 03 '17 at 11:47
2 Answers
13
std::towlower
is the function you want, from <cwtype>
. This header contains many functions for dealing with wide strings.
Example:
// Convert wstring to upper case
wstring wstrTest = L"I am a STL wstring";
transform(
wstrTest.begin(), wstrTest.end(),
wstrTest.begin(),
towlower);

Colin
- 3,394
- 1
- 21
- 29
-
1I don't think this can work correctly with code points that span multiple code units. – eerorika Feb 03 '17 at 10:48
-
@user2079303 C++ (and C here) requires that any supported code point fits in a wchar_t. In other words, that is only a problem on Windows. (The real problem is that case mapping is not 1:1 in code points and is context-dependent, as noted in other comments) – Cubbi Feb 10 '17 at 14:31
-
@Cubbi A windows problems is a problem to most cross platform programs :) But yeah, that is indeed not the only reason why transformation one code-unit at a time cannot work. The mapping algorithm must deal with the strings as a whole to work correctly. – eerorika Feb 10 '17 at 14:55
-
5
5
Hope that helps..
#include <iostream>
#include <algorithm>
int main ()
{
std::wstring str = L"THIS TEXT!";
std::wcout << "Lowercase of the string '" << str << "' is ";
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
std::wcout << "'" << str << "'\n";
return 0;
}
Output :
Lowercase of the string 'THIS TEXT!' is 'this text!'

Konstantinos Monachopoulos
- 1,045
- 2
- 14
- 29
-
-
-
8This answer is wrong and extremely dangerous. Using ::tolower on chars (!) is already dangerous, on wchars it's just asking for a crash. If the value of character passed to ::tolower is not representable as unsigned char and does not equal EOF, the behavior is undefined. It is because implementations can use lookup table of size [256]. If you pass there anything, that is greater than 255 (as wchars can be), it can simply crash. – Kaznov Apr 25 '21 at 00:38