c/c++ trim

use erase and find_if to implement trim

c/c++ trim 实现字符串两头空格删除

#include 
#include 
#include 

inline void trim_left(std::string &str)
{
    str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](unsigned char ch) { return !std::isspace(ch); }));
}

inline void trim_right(std::string &str)
{
    str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), str.end());
}

inline void trim(std::string &str)
{
    trim_left(str);
    trim_right(str);
}

find_if:from start to end find the first element make the third func true and return the iterator

std::find_if(str.begin(), str.end(), [](unsigned char ch) { return !std::isspace(ch); })

你可能感兴趣的:(C++,c++,c语言,trim)