您当前的位置: 首页 >  c++

令狐掌门

暂无认证

  • 2浏览

    0关注

    513博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C++ 去掉字符串的首尾空格和全部空格

令狐掌门 发布时间:2017-12-19 11:23:03 ,浏览量:2

#include 
#include 
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             
关注
打赏
1652240117
查看更多评论
0.0397s