c++去掉首尾空格是参考一篇文章的,但是忘记文章出处了,就略过吧。
去掉首尾空格的代码如下:
void trim(string &s)
{
if( !s.empty() )
{
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
}
}
去掉首尾空格
去掉字符串中所有空格的代码如下:
void trim(string &s)
{
/*
if( !s.empty() )
{
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
}
*/
int index = 0;
if( !s.empty())
{
while( (index = s.find(' ',index)) != string::npos)
{
s.erase(index,1);
}
}
}
去掉所有空格
测试代码如下&#