반응형

[C++] 문자열 앞뒤 공백 제거 방법 ( Boost 라이브러리 안 씀 )

반응형


Boost 라이브러리를 사용하지 않고 문자열 앞뒤 공백 제거할 수 있는 방법을 발견했다. 

고마워요 스택오버플로우

// trim from left
inline std::string& ltrim(std::string& s, const char* t = " \t\n\r\f\v")
{
    s.erase(0, s.find_first_not_of(t));
    return s;
}

// trim from right
inline std::string& rtrim(std::string& s, const char* t = " \t\n\r\f\v")
{
    s.erase(s.find_last_not_of(t) + 1);
    return s;
}

// trim from left & right
inline std::string& trim(std::string& s, const char* t = " \t\n\r\f\v")
{
    return ltrim(rtrim(s, t), t);
}

// copying versions

inline std::string ltrim_copy(std::string s, const char* t = " \t\n\r\f\v")
{
    return ltrim(s, t);
}

inline std::string rtrim_copy(std::string s, const char* t = " \t\n\r\f\v")
{
    return rtrim(s, t);
}

inline std::string trim_copy(std::string s, const char* t = " \t\n\r\f\v")
{
    return trim(s, t);
}


출처 : https://stackoverflow.com/questions/1798112/removing-leading-and-trailing-spaces-from-a-string

반응형

'Language & Solution > C++' 카테고리의 다른 글

[C++] 문자열 뒤집기  (0) 2018.11.20
[C++] 오버플로우(Overflow) 체크방법  (0) 2018.11.20
[C++] namespace, FQN(fully Qualified Name)  (0) 2018.06.24
[C++] constexpr  (0) 2018.06.24
[C++] Header File (헤더 파일)  (0) 2018.06.24

이 글을 공유하기

댓글

Designed by JB FACTORY