This shows you the differences between two versions of the page.
— |
c:string:trim [2019/11/14 23:23] (current) odefta created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Trim a std::string ====== | ||
+ | |||
+ | <code cpp> | ||
+ | std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") | ||
+ | { | ||
+ | str.erase(0, str.find_first_not_of(chars)); | ||
+ | return str; | ||
+ | } | ||
+ | |||
+ | std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") | ||
+ | { | ||
+ | str.erase(str.find_last_not_of(chars) + 1); | ||
+ | return str; | ||
+ | } | ||
+ | |||
+ | std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ") | ||
+ | { | ||
+ | return ltrim(rtrim(str, chars), chars); | ||
+ | } | ||
+ | </code> | ||