1 #include2 #include 3 4 using namespace std; 5 6 int main() 7 { 8 string s1 = "one*two*three";//Given String 9 string s2 = "*";//delimeter10 string s3 = ",";//string to replace11 12 cout << "s1 = " << s1 << endl;//Original String before replace13 14 bool flag = true;15 while(flag)16 {17 size_t found = s1.find(s2);//Stores the size of the string by using find() function18 if(found != string::npos)//its check until the 'n' of the occurence in the given string.19 {20 s1.replace(found, s2.length(), s3);//Replace the string using the replace() function21 }22 else23 {24 flag = false;25 }26 }27 cout << "s1 = " << s1 << endl;//After replacing the string28 29 return 0;30 }
运行结果:
basic_string::replace
将原string 中的元素或子串替换。返回替换后的string。
(1)用string 或C-string 代替操作string 中从 _Pos1 开始的 _Num1 个字符
basic _ string& replace( size _ type _Pos1 ,size _ type _Num1 , const value _ type* _Ptr );
basic _ string& replace(size _ type _Pos1 ,size _ type _Num1 ,const basic _ string _Str );
1 string a,b; 2 3 string s ( "AAAAAAAA" ); 4 5 string s1p ( "BBB" ); 6 7 const char* cs1p = "CCC" ; 8 9 a = s.replace ( 1 , 3 , s1p ); // s= ” ABBBAAAA ”10 11 b = s.replace ( 5 , 3 , cs1p ); // s= ” ABBBACCC ”
(2)用string 中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符
用C-string 中的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符
basic _ string& replace( size _ type _Pos1 , size _ type _Num1 , const basic _ string& _Str ,
size _ type _Pos2 , size _ type _Num2 );
basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,
const value _ type* _Ptr , size _ type _Num2 );
1 string a, b; 2 3 string s ( "AAAAAAAA" ); 4 5 string s2p ( "BBB" ); 6 7 const char* cs2p = "CCC"; 8 9 a = s.replace ( 1 , 3 , s2p , 1 , 2 ); // s= ” ABBAAAA ”10 11 b = s.replace ( 4 , 3 , cs2p , 1 ); // s= ” ABBAC ”
(3)用 _Count 个character _Ch , 代替操作string 中从 _Pos1 开始的 _Num1 个字符
basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,
size _ type _Count , value _ type _Ch );
1 string result;2 3 string s ( "AAAAAAAA" );4 5 char ch = 'C';6 7 result = s.replace ( 1 , 3 , 4 , ch ); // s= ” ACCCCAAAA ”
(4)用string 或C-string ,代替操作string 中从 First0 到 Last0 的字符
basic _ string&replace(iterator First0 ,iterator Last0 , const basic _ string& _Str );
basic _ string&replace(iterator First0 ,iterator _Last0 , const value _ type* _Ptr );
string s ( "AAAAAAAA" ); string s4p ( "BBB" );
const char* cs4p = "CCC";
basic_string<char>::iterator IterF0, IterL0;
IterF0 = s.begin ( ); IterL0 = s.begin ( ) + 3;
string a, b;
a = s.replace ( IterF0 , IterL0 , s4p ); // s= ” BBBAAAAA ”
b = s.replace ( IterF0 , IterL0 , cs4p ); // s= ” CCCAAAAA ”
(5)用string 中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符
用C-string 中的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符
basic _ string& replace( iterator _First0 , iterator _Last0 ,
const value _ type* _Ptr , size _ type _Num2 );
template<class InputIterator> basic _ string& replace(
iterator _First0 , iterator _Last0 ,
InputIterator _First , InputIterator _Last );
IterF3 = s.begin ( ) + 1; IterL3 = s.begin ( ) + 3;
IterF4 = s.begin ( ); IterL4 = s.begin ( ) + 2;
a = s.replace ( IterF3 , IterL3 , IterF4 , IterL4 );
b = s.replace ( IterF1 , IterL1 , cs5p , 4 );
(6)用 _Count 个character _Ch , 代替操作string 中从 First0 到 Last0 的字符
basic _ string& replace( iterator _First0 , iterator _Last0 ,
size _ type _Count , value _ type _Ch );
a = s.replace ( IterF2 , IterL2 , 4 , ch );