C++ 문자열 trim() 구현

1 개요[ | ]

C++ trim() 구현
C++ 문자열 trim() 구현
#include <iostream>
#include <vector>
#include <algorithm> 
using namespace std;

int main() {
    string s = "  Hello, World  ";
    s.erase(s.begin(), find_if(s.begin(), s.end(), [](unsigned char ch) { return !isspace(ch); }));
    s.erase(find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !isspace(ch); }).base(), s.end());
    cout << '[' << s << ']'; // [Hello, World]
}
#include <iostream>
#include <vector>
#include <algorithm> 
using namespace std;

int main() {
    string s = "  Hello, World  ";
    s.erase(s.begin(), find_if(s.begin(), s.end(), [](char ch) { return ch!=' '; }));
    s.erase(find_if(s.rbegin(), s.rend(), [](char ch) { return ch!=' '; }).base(), s.end());
    cout << '[' << s << ']'; // [Hello, World]
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}