What's the best way to trim std::string?

后端 未结 30 2916
无人及你
无人及你 2020-11-21 22:13

I\'m currently using the following code to right-trim all the std::strings in my programs:

std::string s;
s.erase(s.find_last_not_of(\" \\n\\r\\         


        
相关标签:
30条回答
  • 2020-11-21 22:30

    I like tzaman's solution, the only problem with it is that it doesn't trim a string containing only spaces.

    To correct that 1 flaw, add a str.clear() in between the 2 trimmer lines

    std::stringstream trimmer;
    trimmer << str;
    str.clear();
    trimmer >> str;
    
    0 讨论(0)
  • 2020-11-21 22:30

    I guess if you start asking for the "best way" to trim a string, I'd say a good implementation would be one that:

    1. Doesn't allocate temporary strings
    2. Has overloads for in-place trim and copy trim
    3. Can be easily customized to accept different validation sequences / logic

    Obviously there are too many different ways to approach this and it definitely depends on what you actually need. However, the C standard library still has some very useful functions in <string.h>, like memchr. There's a reason why C is still regarded as the best language for IO - its stdlib is pure efficiency.

    inline const char* trim_start(const char* str)
    {
        while (memchr(" \t\n\r", *str, 4))  ++str;
        return str;
    }
    inline const char* trim_end(const char* end)
    {
        while (memchr(" \t\n\r", end[-1], 4)) --end;
        return end;
    }
    inline std::string trim(const char* buffer, int len) // trim a buffer (input?)
    {
        return std::string(trim_start(buffer), trim_end(buffer + len));
    }
    inline void trim_inplace(std::string& str)
    {
        str.assign(trim_start(str.c_str()),
            trim_end(str.c_str() + str.length()));
    }
    
    int main()
    {
        char str [] = "\t \nhello\r \t \n";
    
        string trimmed = trim(str, strlen(str));
        cout << "'" << trimmed << "'" << endl;
    
        system("pause");
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-21 22:31

    EDIT Since c++17, some parts of the standard library were removed. Fortunately, starting with c++11, we have lambdas which are a superior solution.

    #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(), [](unsigned char 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(), [](unsigned char 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;
    }
    

    Thanks to https://stackoverflow.com/a/44973498/524503 for bringing up the modern solution.

    Original answer:

    I tend to use one of these 3 for my trimming needs:

    #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));
    }
    

    They are fairly self-explanatory and work very well.

    EDIT: BTW, I have std::ptr_fun in there to help disambiguate std::isspace because there is actually a second definition which supports locales. This could have been a cast just the same, but I tend to like this better.

    EDIT: To address some comments about accepting a parameter by reference, modifying and returning it. I Agree. An implementation that I would likely prefer would be two sets of functions, one for in place and one which makes a copy. A better set of examples would be:

    #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;
    }
    

    I am keeping the original answer above though for context and in the interest of keeping the high voted answer still available.

    0 讨论(0)
  • 2020-11-21 22:31

    This is what I use. Just keep removing space from the front, and then, if there's anything left, do the same from the back.

    void trim(string& s) {
        while(s.compare(0,1," ")==0)
            s.erase(s.begin()); // remove leading whitespaces
        while(s.size()>0 && s.compare(s.size()-1,1," ")==0)
            s.erase(s.end()-1); // remove trailing whitespaces
    }
    
    0 讨论(0)
  • 2020-11-21 22:32

    Use the following code to right trim (trailing) spaces and tab characters from 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));
    }
    

    And just to balance things out, I'll include the left trim code too (ideone):

    // trim leading spaces
    size_t startpos = str.find_first_not_of(" \t");
    if( string::npos != startpos )
    {
        str = str.substr( startpos );
    }
    
    0 讨论(0)
  • 2020-11-21 22:33

    Trim C++11 implementation:

    static void trim(std::string &s) {
         s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), [](char c){ return std::isspace(c); }));
         s.erase(std::find_if_not(s.rbegin(), s.rend(), [](char c){ return std::isspace(c); }).base(), s.end());
    }
    
    0 讨论(0)
提交回复
热议问题