std :: string을 자르는 가장 좋은 방법은 무엇입니까? 현재 다음 코드를 사용하여 std::strings프로그램의 모든

현재 다음 코드를 사용하여 std::strings프로그램의 모든 항목을 오른쪽으로 자릅니다 .

std::string s;
s.erase(s.find_last_not_of(" \n\r\t")+1);

잘 작동하지만 실패 할 수있는 최종 사례가 있는지 궁금합니다.

물론, 우아한 대안과 왼쪽 손질 솔루션으로 답변을 환영합니다.



답변

편집 c ++ 17 이후 표준 라이브러리의 일부가 제거되었습니다. 다행스럽게도 c ++ 11부터는 우수한 솔루션 인 람다가 있습니다.

#include <algorithm>
#include <cctype>
#include <locale>

// trim from start (in place)
static inline void ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
        return !std::isspace(ch);
    }));
}

// trim from end (in place)
static inline void rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
        return !std::isspace(ch);
    }).base(), s.end());
}

// trim from both ends (in place)
static inline void trim(std::string &s) {
    ltrim(s);
    rtrim(s);
}

// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
    ltrim(s);
    return s;
}

// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
    rtrim(s);
    return s;
}

// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
    trim(s);
    return s;
}

최신 솔루션을 제공하는 https://stackoverflow.com/a/44973498/524503 에 감사합니다 .

원래 답변 :

트리밍 요구에 다음 3 가지 중 하나를 사용하는 경향이 있습니다.

#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>

// trim from start
static inline std::string &ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(),
            std::not1(std::ptr_fun<int, int>(std::isspace))));
    return s;
}

// trim from end
static inline std::string &rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(),
            std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
    return s;
}

// trim from both ends
static inline std::string &trim(std::string &s) {
    return ltrim(rtrim(s));
}

그들은 상당히 자명하고 잘 작동합니다.

편집 : BTW, 실제로 로케일을 지원하는 두 번째 정의가 있기 때문에 std::ptr_fun명확성을 돕기 위해 거기에 std::isspace있습니다. 이것은 똑같은 캐스트 일 수 있었지만 나는 이것을 더 좋아하는 경향이 있습니다.

편집 : 매개 변수를 참조하여 수락하고 수정하고 반환하는 것에 대한 의견을 제시합니다. 동의한다. 내가 선호하는 구현은 두 가지 기능 세트, 하나는 제자리에 있고 다른 하나는 사본을 만듭니다. 더 나은 예는 다음과 같습니다.

#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>

// trim from start (in place)
static inline void ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(),
            std::not1(std::ptr_fun<int, int>(std::isspace))));
}

// trim from end (in place)
static inline void rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(),
            std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}

// trim from both ends (in place)
static inline void trim(std::string &s) {
    ltrim(s);
    rtrim(s);
}

// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
    ltrim(s);
    return s;
}

// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
    rtrim(s);
    return s;
}

// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
    trim(s);
    return s;
}

나는 문맥 상으로 그리고 높은 투표 응답을 계속 사용할 수 있도록 위의 원래 답변을 유지하고 있습니다.


답변

사용 부스트의 문자열 알고리즘은 쉬운 것입니다 :

#include <boost/algorithm/string.hpp>

std::string str("hello world! ");
boost::trim_right(str);

str지금 "hello world!"입니다. 이이기도 trim_left하고 trim양쪽 트림한다.


_copy위의 함수 이름 에 접미사를 추가하면 ( 예 : 참조 trim_copy) 함수는 문자열을 참조를 통해 수정하지 않고 잘린 사본을 반환합니다.

_if예를 들어 위의 함수 이름 에 접미사를 추가 trim_copy_if하면 공백이 아닌 사용자 지정 술어를 만족시키는 모든 문자를자를 수 있습니다.


답변

다음 코드를 사용하여 std::strings( ideone ) 에서 공백과 탭 문자를 오른쪽으로 자르십시오 (트레일 링 ).

// trim trailing spaces
size_t endpos = str.find_last_not_of(" \t");
size_t startpos = str.find_first_not_of(" \t");
if( std::string::npos != endpos )
{
    str = str.substr( 0, endpos+1 );
    str = str.substr( startpos );
}
else {
    str.erase(std::remove(std::begin(str), std::end(str), ' '), std::end(str));
}

그리고 균형을 맞추기 위해 왼쪽 트림 코드도 포함합니다 ( ideone ).

// trim leading spaces
size_t startpos = str.find_first_not_of(" \t");
if( string::npos != startpos )
{
    str = str.substr( startpos );
}

답변

당신이하고있는 일은 훌륭하고 강력합니다. 나는 같은 방법을 오랫동안 사용했지만 아직 더 빠른 방법을 찾지 못했습니다.

const char* ws = " \t\n\r\f\v";

// trim from end of string (right)
inline std::string& rtrim(std::string& s, const char* t = ws)
{
    s.erase(s.find_last_not_of(t) + 1);
    return s;
}

// trim from beginning of string (left)
inline std::string& ltrim(std::string& s, const char* t = ws)
{
    s.erase(0, s.find_first_not_of(t));
    return s;
}

// trim from both ends of string (right then left)
inline std::string& trim(std::string& s, const char* t = ws)
{
    return ltrim(rtrim(s, t), t);
}

트리밍 할 문자를 제공하면 공백이 아닌 문자를 트리밍 할 수있는 유연성과 트리밍하려는 문자 만 트리밍 할 수있는 효율성이 있습니다.


답변

파티에 늦었지만 걱정하지 마십시오. 이제 C ++ 11이 있습니다. 람다와 자동 변수가 있습니다. 따라서 모든 공백과 빈 문자열을 처리하는 내 버전은 다음과 같습니다.

#include <cctype>
#include <string>
#include <algorithm>

inline std::string trim(const std::string &s)
{
   auto wsfront=std::find_if_not(s.begin(),s.end(),[](int c){return std::isspace(c);});
   auto wsback=std::find_if_not(s.rbegin(),s.rend(),[](int c){return std::isspace(c);}).base();
   return (wsback<=wsfront ? std::string() : std::string(wsfront,wsback));
}

우리는 역 반복자를 만들 수 있습니다. wsfront 두 번째 터미네이션 조건으로 사용할 수 find_if_not있지만 공백 공백 문자열의 경우에만 유용하며 gcc 4.8 이상은 리버스 이터레이터의 유형을 유추하기에 충분하지 않습니다 ( std::string::const_reverse_iterator)와 auto. 역 이터레이터를 구성하는 데 비용이 많이 들지 않으므로 YMMV입니다. 이 변경으로 코드는 다음과 같습니다.

inline std::string trim(const std::string &s)
{
   auto  wsfront=std::find_if_not(s.begin(),s.end(),[](int c){return std::isspace(c);});
   return std::string(wsfront,std::find_if_not(s.rbegin(),std::string::const_reverse_iterator(wsfront),[](int c){return std::isspace(c);}).base());
}

답변

이것을 시도하십시오, 그것은 나를 위해 작동합니다.

inline std::string trim(std::string& str)
{
    str.erase(0, str.find_first_not_of(' '));       //prefixing spaces
    str.erase(str.find_last_not_of(' ')+1);         //surfixing spaces
    return str;
}

답변

나는 tzaman의 솔루션을 좋아하는데, 유일한 문제는 공백 만 포함하는 문자열을 자르지 않는다는 것입니다.

1 개의 결함을 수정하려면 2 개의 트리머 라인 사이에 str.clear ()를 추가하십시오.

std::stringstream trimmer;
trimmer << str;
str.clear();
trimmer >> str;