C++字符串中删除所有指定的字符

版权声明:版权声明:本文为博主原创文章,博客地址:http://blog.csdn.net/weixin_41969587,未经博主允许不得转载。 https://blog.csdn.net/weixin_41969587/article/details/82587250

如何删除字符串中所有指定的字符?

用erase()函数即可:

#include<iostream>
#include<string.h>
using namespace std;
int main(){
    string s="-daas-j--kdj-al-";
    string::iterator it;
    for(it=s.begin();it!=s.end();it++)
    if(*it=='-'){
    s.erase(it);
    it--;
}
    cout<<s<<endl;
    return 0;
}

程序运行的结果为:

这里写图片描述

代码如有错误,欢迎大家指出来!

猜你喜欢

转载自blog.csdn.net/weixin_41969587/article/details/82587250
今日推荐