Skip to content

Instantly share code, notes, and snippets.

@blippy
Created March 15, 2016 12:14
Show Gist options
  • Save blippy/dc2d8c0a09a5ef7b0a61 to your computer and use it in GitHub Desktop.
Save blippy/dc2d8c0a09a5ef7b0a61 to your computer and use it in GitHub Desktop.
trim a string of a set of characters
const char white[] = " \t\r";
string trim(string& str)
{
if(str.length() ==0) { return str;}
size_t first = str.find_first_not_of(white);
if(first == string::npos) return "";
size_t last = str.find_last_not_of(white);
return str.substr(first, (last-first+1));
}
@blippy
Copy link
Author

blippy commented Mar 17, 2016

Note that it trims the left and right extremities of a string, not the whole string itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment