std::string不支持trim方法,同时原生的replace方法在使用上和其它语言也有些许差别,这里提供了两个替代方法,个人感觉更加好用。std::wstring等类似。
static int(__cdecl is_space)(int const c)
{
return (c >= -127 && c <= 127 && isspace(c) != 0);
}
//-------------------------------------------------------------------------
static void trim(std::string &s)
{
// trim from start (in place)
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(is_space))));
// trim from end (in place)
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(is_space))).base(), s.end());
}
//-------------------------------------------------------------------------
static void replace(std::string &s,
const char *p1, int l1,
const char *p2, int l2)
{
std::string::size_type pos = 0;
while ((pos = s.find(p1, pos)) != std::string::npos) {
s.replace(pos, l1, p2);
pos = pos + l2;
}
}