Created
October 14, 2021 19:25
-
-
Save hechen/b9660c3c4e1051222dd66bdfc39b7858 to your computer and use it in GitHub Desktop.
Add trim support for c++ string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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