#include <iostream>
#include <string>
using namespace std;


//去掉收尾空格
string& ClearHeadTailSpace(string &str)   
{  
if (str.empty())   
{  
return str;  
}  

str.erase(0,str.find_first_not_of(" "));  
str.erase(str.find_last_not_of(" ") + 1);  
return str;  
}  


//去掉字符串中的全部空格
string& ClearAllSpace(string &str)
{
    int index = 0;
    if( !str.empty())
    {
        while( (index = str.find(' ',index)) != string::npos)
        {
            str.erase(index,1);
        }
    }

return str;
}


int main()
{
string str = "  123   456  789   ";

cout << ClearHeadTailSpace(str) << endl;
cout << ClearAllSpace(str) << endl;

system("pause");
return 0;
}
————————————————

原文链接:https://blog.csdn.net/yao_hou/article/details/78840723

相关文章:

  • 2021-07-29
  • 2021-10-25
  • 2021-12-09
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-10
  • 2022-12-23
  • 2022-02-07
  • 2022-01-02
  • 2021-11-16
相关资源
相似解决方案