Skip to content

Instantly share code, notes, and snippets.

@andr1972
Created August 23, 2022 12:22
Show Gist options
  • Save andr1972/5ea5099a33d78a657dd55799f12cb0f3 to your computer and use it in GitHub Desktop.
Save andr1972/5ea5099a33d78a657dd55799f12cb0f3 to your computer and use it in GitHub Desktop.
Trim line in C++
#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