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

后端 未结 30 2900
无人及你
无人及你 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:30

    I like tzaman's solution, the only problem with it is that it doesn't trim a string containing only spaces.

    To correct that 1 flaw, add a str.clear() in between the 2 trimmer lines

    std::stringstream trimmer;
    trimmer << str;
    str.clear();
    trimmer >> str;
    

提交回复
热议问题