8

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?

Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
msing
  • 81
  • 1
  • 1
  • 2
  • 2
    http://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
  • 1
    In 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 Answers2

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
  • 1
    I 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
    `` didn't work for me, but `` worked. – Charles Milette Dec 27 '17 at 21:03
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!'
  • Works great with this method ta – Epirocks Oct 03 '17 at 15:51
  • This with I do not require BOOST. Thanks! – Rocky Scott Aug 23 '20 at 20:43
  • 8
    This 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