Given string foo
, I've written answers on how to use cctype
's tolower
to convert the characters to lowercase
transform(cbegin(foo), cend(foo), begin(foo), static_cast<int (*)(int)>(tolower))
But I've begun to consider locale
's tolower
, which could be used like this:
use_facet<ctype<char>>(cout.getloc()).tolower(data(foo), next(data(foo), foo.size()));
tolower
accepts and returns an int
which I assume is just some antiquated C stuff?Description. The C library function int tolower(int c) converts a given letter to lowercase.
To convert a given string to lowercase in C language, iterate over characters of the string and convert each character to lowercase using tolower() function.
The tolower() function is defined in the ctype. h header file.
ToLower(Char)Converts the value of a Unicode character to its lowercase equivalent.
Unfortunately,both are equally bad. Although std::string
pretends to be a utf-8 encoded string, non of the methods/function (including tolower), are really utf-8 aware. So, tolower
/ tolower
+ locale may work with characters which are single byte (= ASCII), they will fail for every other set of languages.
On Linux, I'd use ICU library. On Windows, I'd use CharUpper
function.
In the first case (cctype) the locale is set implicitely:
Converts the given character to lowercase according to the character conversion rules defined by the currently installed C locale.
http://en.cppreference.com/w/cpp/string/byte/tolower
In the second (locale's) case you have to explicitely set the locale:
Converts parameter c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent, as determined by the ctype facet of locale loc. If no such conversion is possible, the value returned is c unchanged.
http://www.cplusplus.com/reference/locale/tolower/
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