Last active
December 29, 2015 20:19
-
-
Save superwills/7723313 to your computer and use it in GitHub Desktop.
std::string::trimExtensions for std::string to trim() whitespace.
This file contains 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
// Extensions for std::string to trim whitespace | |
string& trimR( string& str ) | |
{ | |
if( !str.size() ) return str; | |
// find the last whitespace chr, then delete from there to end. | |
string::iterator iter = --str.end() ; | |
while( isspace( *iter ) ) --iter; | |
str.erase( ++iter, str.end() ) ; | |
return str ; | |
} | |
string& trimL( string& str ) | |
{ | |
if( !str.size() ) return str; | |
// find first non-whitespace, then delete from begin to there. | |
string::iterator iter = str.begin() ; | |
while( iter != str.end() && isspace( *iter ) ) ++iter; | |
str.erase( str.begin(), iter ) ; | |
return str ; | |
} | |
string& trim( string& str ) | |
{ | |
return trimR( trimL( str ) ) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment