读书人

Trim a string with C++ [二]

发布时间: 2012-11-26 11:48:49 作者: rapoo

Trim a string with C++ [2]

I've wrote a blog to discuss how to trim a string in C++. Now I find an other way in high performance, see the implements below:

#include <string>#define  STRING_TRIM_DROPS    " \t"std::string& trim(std::string &s){    static std::string drops = STRING_TRIM_DROPS;  // A char list specifies which characters should be trimmed.    if (s.empty())    {        return s;    }    s.erase(0, s.find_first_not_of(drops));    s.erase(s.find_last_not_of(drops) + 1);    return s;}

Yes, using string::erase() method is the fastest way I ever met.

读书人网 >C++

热点推荐