string a;//在指定串里查找
string b;//要查找的串
string c;//要替换成的新串
cin>>a>>b>>c;
int pos;
pos = a.find(b);//查找指定的串
while (pos != -1)
{
a.replace(pos,b.length(),c);//用新的串替换掉指定的串
pos = a.find(b);//继续查找指定的串,直到所有的都找到为止
}
cout<<a<<endl;
C++:查找字符串子串并替换
最新推荐文章于 2025-05-13 23:32:40 发布