toLowerCase()
and toUpperCase()
, C++ std::string
does not have such a utility method. Instead, you need to use the std::transform()
function. Here’s some example code:#include <iostream> #include <string> #include <utility> int main(int argc, char* argv[]) { std::string s1 = "lowertoupper"; std::string s2 = "UPPERTOLOWER"; std::cout << s1 << " : "; std::transform(s1.begin(), s1.end(), s1.begin(), ::toupper); std::cout << s1 << std::endl; std::cout << s2 << " : "; std::transform(s2.begin(), s2.end(), s2.begin(), ::tolower); std::cout << s2 << std::endl; return 0; }Produces the following:
$ ./case.exe lowertoupper : LOWERTOUPPER UPPERTOLOWER : uppertolowerNote that while the Java
toUpperCase()
and toLowerCase()
methods do not modify the original string, the std::transform
function does.Source URL: http://blog.fourthwoods.com/2013/12/10/convert-c-string-to-lower-case-or-upper-case/
0 comments:
Post a Comment