Skip to content

Instantly share code, notes, and snippets.

@chiihuang
Created May 6, 2017 05:14
Show Gist options
  • Save chiihuang/699fee8617c0b28025c24abf5018be7d to your computer and use it in GitHub Desktop.
Save chiihuang/699fee8617c0b28025c24abf5018be7d to your computer and use it in GitHub Desktop.
How to trim a string in C++
#include <iostream>
#include <string>
using namespace std;
string trim(string& str)
{
size_t first = str.find_first_not_of(' ');
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last-first+1));
}
int main() {
string s = "abc ";
cout << trim(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment