I tend to use one of these 3 for my trimming needs:
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
// trim from start
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
They are fairly self explanatory and work very well.
EDIT: btw, I have std::ptr_fun
in there to help disambiguate std::isspace
because there is actually a second definition which supports locales. This could have been a cast just the same, but I tend to like this better.