Skip to content

Instantly share code, notes, and snippets.

@superwills
Last active December 29, 2015 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superwills/7723313 to your computer and use it in GitHub Desktop.
Save superwills/7723313 to your computer and use it in GitHub Desktop.
std::string::trimExtensions for std::string to trim() whitespace.
// 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