C++实现字符串替换的两种方法

时间:2025-03-15 07:15:02
//C++ 第一种替换字符串的方法用replace()|C++ 第二种替换字符串的方法用erase()和insert()【 C++string|C++ replace()|C++ erase()|C++ insert()|C++自定义替换字符串函数】 #include<string> #include<iostream> using namespace std; //第一种替换字符串的方法用replace() void string_replace(string&s1,const string&s2,const string&s3) { string::size_type pos=0; string::size_type a=(); string::size_type b=(); while((pos=(s2,pos))!=string::npos) { (pos,a,s3); pos+=b; } } //第二种替换字符串的方法用erase()和insert() void string_replace_2(string&s1,const string&s2,const string&s3) { string::size_type pos=0; string::size_type a=(); string::size_type b=(); while((pos=(s2,pos))!=string::npos) { (pos,a); (pos,s3); pos+=b; } }