How to convert std::string to lower case?

后端 未结 26 1673
旧时难觅i
旧时难觅i 2020-11-22 00:01

I want to convert a std::string to lowercase. I am aware of the function tolower(), however in the past I have had issues with this function and it

26条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 00:23

    Here's a macro technique if you want something simple:

    #define STRTOLOWER(x) std::transform (x.begin(), x.end(), x.begin(), ::tolower)
    #define STRTOUPPER(x) std::transform (x.begin(), x.end(), x.begin(), ::toupper)
    #define STRTOUCFIRST(x) std::transform (x.begin(), x.begin()+1, x.begin(),  ::toupper); std::transform (x.begin()+1, x.end(),   x.begin()+1,::tolower)
    

    However, note that @AndreasSpindler's comment on this answer still is an important consideration, however, if you're working on something that isn't just ASCII characters.

提交回复
热议问题