Created
May 6, 2017 05:14
-
-
Save chiihuang/699fee8617c0b28025c24abf5018be7d to your computer and use it in GitHub Desktop.
How to trim a string in C++
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
#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