The C++ basic_string class member, replace() part II code example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Replacing the elements in a string at a specified position with specified characters or characters copied from other ranges or strings or C-strings using replace(), the basic_string class member in C++ programming
To show: How to use the replace(), the basic_string class member in C++ programming part II
// the C++ replace() example part II
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
// replacing part of the string, delineated with iterators, with a string or C-string
string str11, str12;
string str13("TESTING1");
string str14("123");
const char* cstr3 ="AAA";
cout<<"str13 string is: "<<str13<<endl;
cout<<"str14 string is: "<<str14<<endl;
cout<<"cstr3 C-string is: "<<cstr3<<endl;
basic_string<char>::iterator Iter1, Iter2;
cout<<"Operation: str13.begin()"<<endl;
cout<<"Operation: str13.begin() + 3"<<endl;
cout<<"Operation: str13.replace(Iter1, Iter2, str14)"<<endl;
Iter1 = str13.begin();
Iter2 = str13.begin() + 3;
str11 = str13.replace(Iter1, Iter2, str14);
cout<<"The new string is: "<<str11<<endl;
cout<<"Operation: str13.replace(Iter1, Iter2, cstr3)"<<endl;
str12 = str13.replace(Iter1, Iter2, cstr3);
cout<<"The new string is: "<<str12<<endl<<endl;
// replacing part of the string delineated with iterators with a number of C-string characters
string str15;
string str16("TESTING2");
const char* cstr4 ="1234AA";
cout<<"str16 string is: "<<str16<<endl;
cout<<"cstr4 C-string is: "<<cstr4<<endl;
basic_string<char>::iterator Iter3, Iter4;
cout<<"Operation: str16.begin()"<<endl;
cout<<"Operation: str16.begin() + 4"<<endl;
cout<<"Operation: str16.replace(Iter3, Iter4, cstr4, 4)"<<endl;
Iter3 = str16.begin();
Iter4 = str16.begin() + 4;
str15 = str16.replace(Iter3, Iter4, cstr4, 4);
cout<<"The new string is: "<<str15<<endl<<endl;
// replacing part of the string delineated with iterators with specified characters
string str17;
string str18("TESTING3");
char cstr5 = 'u';
cout<<"str18 string is: "<<str18<<endl;
cout<<"cstr5 character is: "<<cstr5<<endl;
basic_string<char>::iterator Iter5, Iter6;
Iter5 = str18.begin();
Iter6 = str18.begin() + 3;
str17 = str18.replace(Iter5, Iter6, 4, cstr5);
cout<<"The new string is: "<<str17<<endl<<endl;
// replacing part of the operand string delineated with iterators with part of a parameter string delineated with iterators
string str19;
string str20("TESTING4"); // operand
string str21("1234"); // parameter
cout<<"str20 string is: "<<str20<<endl;
cout<<"str21 string is: "<<str21<<endl;
basic_string<char>::iterator Iter7, Iter8, Iter9, Iter10;
cout<<"Operation: str20.begin() + 1"<<endl;
cout<<"Operation: str20.begin() + 3"<<endl;
cout<<"Operation: str21.begin()"<<endl;
cout<<"Operation: str21.begin() + 2"<<endl;
Iter7 = str20.begin() + 1;
Iter8 = str20.begin() + 3;
Iter9 = str21.begin();
Iter10 = str21.begin() + 2;
cout<<"Operation: str20.replace(Iter7, Iter8, Iter9, Iter10)"<<endl;
str19 = str20.replace(Iter7, Iter8, Iter9, Iter10);
cout<<"The new string is: "<<str19<<endl;
return 0;
}
Output example:
str13 string is: TESTING1
str14 string is: 123
cstr3 C-string is: AAA
Operation: str13.begin()
Operation: str13.begin() + 3
Operation: str13.replace(Iter1, Iter2, str14)
The new string is: 123TING1
Operation: str13.replace(Iter1, Iter2, cstr3)
The new string is: AAATING1
str16 string is: TESTING2
cstr4 C-string is: 1234AA
Operation: str16.begin()
Operation: str16.begin() + 4
Operation: str16.replace(Iter3, Iter4, cstr4, 4)
The new string is: 1234ING2
str18 string is: TESTING3
cstr5 character is: u
The new string is: uuuuTING3
str20 string is: TESTING4
str21 string is: 1234
Operation: str20.begin() + 1
Operation: str20.begin() + 3
Operation: str21.begin()
Operation: str21.begin() + 2
Operation: str20.replace(Iter7, Iter8, Iter9, Iter10)
The new string is: T12TING4
Press any key to continue . . .