更新时间:2022-05-31 06:01:09
从 http://notfaq.wordpress.com/ 2007/08/04 / cc-convert-string-to-lowerlower-case / :
#include <algorithm>
#include <string>
std::string data = "Abc";
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
你真的不会离开不迭代每个字符。无法知道字符是小写还是大写。
You're really not going to get away with not iterating through each character. There's no way to know whether the character is lowercase or uppercase otherwise.
如果你真的讨厌tolower(),这里是一个不可移植的替代,我不推荐你使用:
If you really hate tolower(), here's a non-portable alternative that I don't recommend you use:
char easytolower(char in){
if(in<='Z' && in>='A')
return in-('Z'-'z');
return in;
}
std::transform(data.begin(), data.end(), data.begin(), easytolower);
请注意 :: tolower()
只能进行单字节字符替换,这对于许多脚本来说是不合适的,尤其是在使用UTF-8等多字节编码时。
Be aware that ::tolower()
can only do a per-single-byte-character substitution, which is ill-fitting for many scripts, especially if using a multi-byte-encoding like UTF-8.