#include <iostream>
#include <string>
using namespace std;
void remove_sub_string(string& src, string& target)
{
size_t pos = src.find(target);
if(pos != string::npos){
src.erase(pos, target.size());
cout << "the pos is :" <<pos <<endl;
cout << "the end pos is: " <<pos + target.size() - 1 << endl;
}
}
int main()
{
string src("hello world!");
string target1("world");
string target2("ih");
cout << "the src string :" << src << endl;
remove_sub_string(src, target1);
cout << "remove the traget1 : " << src << endl;
remove_sub_string(src, target2);
cout << "remove the target2: " << src << endl;
return 0;
}
写一个函数从string中删除一个sub string
最新推荐文章于 2024-01-27 23:17:37 发布