void EraseSpace(string &s)
{
//ch可换成其他字符
const char ch = ' ';
s.erase(s.find_last_not_of(" ") + 1);
s.erase(0, s.find_first_not_of(" "));
}
本文介绍了一种使用 C++ 标准库中的 string 类来删除字符串首尾空格的方法。通过 find_first_not_of 和 find_last_not_of 成员函数找到非空格字符的位置,并据此截取字符串。
void EraseSpace(string &s)
{
//ch可换成其他字符
const char ch = ' ';
s.erase(s.find_last_not_of(" ") + 1);
s.erase(0, s.find_first_not_of(" "));
}
890

被折叠的 条评论
为什么被折叠?