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

后端 未结 30 2880
无人及你
无人及你 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:41

    This version trims internal whitespace and non-alphanumerics:

    static inline std::string &trimAll(std::string &s)
    {   
        if(s.size() == 0)
        {
            return s;
        }
    
        int val = 0;
        for (int cur = 0; cur < s.size(); cur++)
        {
            if(s[cur] != ' ' && std::isalnum(s[cur]))
            {
                s[val] = s[cur];
                val++;
            }
        }
        s.resize(val);
        return s;
    }
    
    0 讨论(0)
  • 2020-11-21 22:42

    http://ideone.com/nFVtEo

    std::string trim(const std::string &s)
    {
        std::string::const_iterator it = s.begin();
        while (it != s.end() && isspace(*it))
            it++;
    
        std::string::const_reverse_iterator rit = s.rbegin();
        while (rit.base() != it && isspace(*rit))
            rit++;
    
        return std::string(it, rit.base());
    }
    
    0 讨论(0)
  • 2020-11-21 22:42

    Here is my version:

    size_t beg = s.find_first_not_of(" \r\n");
    return (beg == string::npos) ? "" : in.substr(beg, s.find_last_not_of(" \r\n") - beg);
    
    0 讨论(0)
  • 2020-11-21 22:42

    Here is a straight forward implementation. For such a simple operation, you probably should not be using any special constructs. The build-in isspace() function takes care of various forms of white characters, so we should take advantage of it. You also have to consider special cases where the string is empty or simply a bunch of spaces. Trim left or right could be derived from the following code.

    string trimSpace(const string &str) {
       if (str.empty()) return str;
       string::size_type i,j;
       i=0;
       while (i<str.size() && isspace(str[i])) ++i;
       if (i == str.size())
          return string(); // empty string
       j = str.size() - 1;
       //while (j>0 && isspace(str[j])) --j; // the j>0 check is not needed
       while (isspace(str[j])) --j
       return str.substr(i, j-i+1);
    }
    
    0 讨论(0)
  • 2020-11-21 22:43

    What you are doing is fine and robust. I have used the same method for a long time and I have yet to find a faster method:

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

    By supplying the characters to be trimmed you have the flexibility to trim non-whitespace characters and the efficiency to trim only the characters you want trimmed.

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

    Try this, it works for me.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题