What's the best way to trim std::string?

后端 未结 30 2913
无人及你
无人及你 2020-11-21 22:13

I\'m currently using the following code to right-trim all the std::strings in my programs:

std::string s;
s.erase(s.find_last_not_of(\" \\n\\r\\         


        
30条回答
  •  醉酒成梦
    2020-11-21 22:33

    Trim C++11 implementation:

    static void trim(std::string &s) {
         s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), [](char c){ return std::isspace(c); }));
         s.erase(std::find_if_not(s.rbegin(), s.rend(), [](char c){ return std::isspace(c); }).base(), s.end());
    }
    

提交回复
热议问题