Skip to content

Instantly share code, notes, and snippets.

@hechen
Created October 14, 2021 19:25
Show Gist options
  • Save hechen/b9660c3c4e1051222dd66bdfc39b7858 to your computer and use it in GitHub Desktop.
Save hechen/b9660c3c4e1051222dd66bdfc39b7858 to your computer and use it in GitHub Desktop.
Add trim support for c++ string
const std::string WHITESPACE = " \n\r\t\f\v";
std::string ltrim(const std::string &s) {
size_t start = s.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : s.substr(start);
}
std::string rtrim(const std::string &s) {
size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
std::string trim(const std::string &s) {
return rtrim(ltrim(s));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment