C++中Trim的方法

C++中Trim的方法,其实也是写的很Trick的……

参考:http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring

#include <stringstream>

void trim(::std::string & str)
{
    //left
    str.erase(str.begin(), std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
    //right
    str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end());
}

Leave a Reply

Your email address will not be published. Required fields are marked *