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

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

    Here's what I came up with:

    std::stringstream trimmer;
    trimmer << str;
    trimmer >> str;
    

    Stream extraction eliminates whitespace automatically, so this works like a charm.
    Pretty clean and elegant too, if I do say so myself. ;)

提交回复
热议问题