The C++ replace() function, the basic_string class member code sample part I

 

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 the basic_string class member, replace() in C++ programming

To show: How to replace 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() member in C++ programming part I

 

// the C++ replace() part I

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// replacing part of the string with characters of a string or C-string remember that index start from 0!

string str1, str2;

string str3("TESTING");

string str4("ABC");

const char* cstr = "DEF";

 

cout<<"str3 string is: "<<str3<<endl;

cout<<"str4 string is: "<<str4<<endl;

cout<<"cstr C-string is: "<<cstr<<endl;

 

str1 = str3.replace(1, 3, str4);

cout<<"Operation: str3.replace(1, 3, str4)"<<endl;

cout<<"The new string is: "<<str1<<endl;

cout<<"\nOperation: str3.replace(5, 3, cstr)"<<endl;

str2 = str3.replace(5, 3, cstr);

cout<<"The new string is: "<<str2<<endl<<endl;

 

// replacing part of the string with characters form part of a string or C-string

string str5, str6;

string str7 ("TESTING");

string str8 ("123");

const char* cstr1 = "456";

 

cout<<"str7 string is: "<<str7<<endl;

cout<<"str8 string is: "<<str8<<endl;

cout<<"cstr1 C-string is: "<<cstr1<<endl;

cout<<"Operation: str7.replace(1, 3, str8, 1, 2)"<<endl;

str5 = str7.replace(1, 3, str8, 1, 2);

cout<<"The new string is: "<<str5<<endl;

cout<<"\nOperation: str7.replace(4, 3, cstr1, 1)"<<endl;

str6 = str7.replace(4, 3, cstr1, 1);

cout<<"The new string is: "<<str6<<endl<<endl;

 

// replacing part of the string with characters

string str9;

string str10 ("TESTING");

char cstr2 = 'R';

cout<<"str10 string is: "<<str10<<endl;

cout<<"cstr2 character is: "<<cstr2<<endl;

cout<<"Operation: str10.replace(2, 4, 5, cstr2)"<<endl;

str9 = str10.replace(2, 4, 5, cstr2);

cout<<"The new string is: "<<str9<<endl;

 

return 0;

}

 

Output example:

 

str3 string is: TESTING

str4 string is: ABC

cstr C-string is: DEF

Operation: str3.replace(1, 3, str4)

The new string is: TABCING

 

Operation: str3.replace(5, 3, cstr)

The new string is: TABCIDEF

 

str7 string is: TESTING

str8 string is: 123

cstr1 C-string is: 456

Operation: str7.replace(1, 3, str8, 1, 2)

The new string is: T23ING

 

Operation: str7.replace(4, 3, cstr1, 1)

The new string is: T23I4

 

str10 string is: TESTING

cstr2 character is: R

Operation: str10.replace(2, 4, 5, cstr2)

The new string is: TERRRRRG

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index