Created
August 23, 2022 12:22
-
-
Save andr1972/5ea5099a33d78a657dd55799f12cb0f3 to your computer and use it in GitHub Desktop.
Trim line in C++
This file contains hidden or 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> | |
using namespace std; | |
string trimLeft(const string& str) | |
{ | |
const auto strBegin = str.find_first_not_of(" \t"); | |
return str.substr(strBegin, str.length() - strBegin); | |
} | |
string trimRight(const string& str) | |
{ | |
const auto strEnd = str.find_last_not_of(" \t\r"); | |
return str.substr(0, strEnd + 1); | |
} | |
string trim(const string& str) { | |
return trimLeft(trimRight(str)); | |
} | |
int main() { | |
const string str = " ala "; | |
cout << "--" << str << "--" << endl; | |
cout << "--" << trimLeft(str) << "--" << endl; | |
cout << "--" << trimRight(str) << "--" << endl; | |
cout << "--" << trim(str) << "--" << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment