Replaces an element or substring in the original string. Returns the replaced string.
(1) using string or c-string instead of the _NUM1 characters starting from _pos1 in the operation string
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);
(2) _num2 characters starting with _pos2 in string instead of _num1 characters starting from _POS1 in operation string
_num2 characters in c-string instead of _NUM1 characters starting from _pos1 in the operation string
Basic _ string& Replace (size _ type _POS1, size _ type _NUM1, const basic _ string& _str,
Size _ Type _pos2, size _ type);
Basic _ string& Replace (size _ type _POS1, size _ type _NUM1,
Const value _ type* _ptr, size _ type _num2);
(3) Use _count character _ch instead of _num1 characters starting from _pos1 in the operation string
Basic _ string& Replace (size _ type _POS1, size _ type _NUM1,
Size _ Type _count, value _ type _ch);
string result;
String S ("aaaaaaaa");
char ch = ' C ';
result = S.replace (1, 3, 4, CH); S= "ACCCCAAAA"
(4) Use string or c-string to replace characters from First0 to Last0 in the operation string
Basic _ String&replace (iterator First0, iterator Last0, const basic _ string& _STR);
Basic _ String&replace (iterator First0, iterator _last0, const value _ type* _ptr);
(5) _num2 characters starting with _pos2 in string instead of First0 to Last0 characters in the operation string
Replaces the characters from First0 to Last0 in the operation string with the _num2 characters in c-string
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) using _count character _ch instead of the characters from First0 to Last0 in the operation string
Basic _ string& Replace (iterator _first0, iterator _last0,
Size _ Type _count, value _ type _ch);
A = S.replace (IterF2, IterL2, 4, ch);
Functions that are often used:
Test1.cpp: Defines the entry point for a console application.
#include "stdafx.h" #include <iostream> #include <string> using namespace std; string& Replace_all (string& str,const string& old_value,const string& new_value) {Whil
E (True) {String::size_type pos (0);
if ((Pos=str.find (Old_value))!=string::npos) str.replace (Pos,old_value.length (), new_value);
else break;
return str; } string& replace_all_distinct (string& str,const string& old_value,const string& new_value {for (String::size_type pos (0); Pos!=string::npos; Pos+=new_value.length ()) {if ((Pos=str.find (Old_value,pos))!=string::npos) str.replace (pos,old_value.le
Ngth (), new_value);
else break;
return str;
int main () {//cout<<str;
String str = "Lihan";
Str.replace (Str.begin (), Str.end () -1,2, ' s ');
cout<<str; cout << Replace_all (String ("12212"), "2", "3") << Endl;
cout << replace_all_distinct (String ("12212"), "a", "a" << Endl;
}