Skip to content

Instantly share code, notes, and snippets.

@andijcr
Created April 3, 2019 11:54
Show Gist options
  • Save andijcr/6429363f7946eaedfb6f7b9f1b4c091a to your computer and use it in GitHub Desktop.
Save andijcr/6429363f7946eaedfb6f7b9f1b4c091a to your computer and use it in GitHub Desktop.
simple in-place trim functions, copied from somewhere
// trim from start (in place)
void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
[](int ch) { return !std::isspace(ch); }));
}
// trim from end (in place)
void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[](int ch) { return !std::isspace(ch); })
.base(),
s.end());
}
// trim from both ends (in place)
void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment