String Replace Function - C++ Programming Tutorial
C++ Course / Strings / String Replace Function

String Replace Function

BLUF: Mastering String Replace Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: String Replace Function

C++ is renowned for its efficiency. Learn how String Replace Function enables low-level control and high-performance computing in the tutorial below.

C++ String replace

This function substitutes the section of a string that starts at character position pos and covers a length of len characters.

Syntax

Consider two strings str1 and str2. The syntax is as follows:

Example

str1.replace(pos,len,str2);

Parameters

  • str : str is a string object, whose value to be copied in another string object.
  • pos : pos defines the position, whose character to be replaced.
  • len : Number of characters to be replaced by another string object.
  • subpos : It defines the position of the first character of string object that is to be copied to another object as replacement.
  • sublen : Number of characters of string object to be copied into another string object.
  • n : Number of characters to be copied into an another string object.
  • Return value

This function does not return any value.

Example 1

The initial example demonstrates the process of replacing a specified string by utilizing position and length as parameters.

Example

#include<iostream>
using namespace std;
int  main()
{
string str1 = "This is C language";
string str2 = "C++";
cout << "Before replacement, string is :"<<str1<<'\n';
str1.replace(8,1,str2); 
cout << "After replacement, string is :"<<str1<<'\n';
return 0;
}

Output:

Output

Before replacement , string is This is C language
After replacement, string is This is C++ language

Example 2

The following example demonstrates how to substitute a specified string by utilizing the position and length of the string that is to be duplicated into another string object.

Example

#include<iostream>
using namespace std;
int main()
{
string str1 ="This is C language"
 string str3= "java language";
cout <<"Before replacement, String is "<<str1<<'\n';
str1.replace(8,1,str3,0,4); 
cout<<"After  replacement,String is "<<str1<<'\n';
return 0;
}

Output:

Output

Before replacement, String is This is C language
After replacement, String is This is java language

Example 3

Third demonstration illustrates how to substitute a string by specifying both the string and the number of characters to be copied as arguments.

Example

#include<iostream>
using namespace std;
int main()
{
string str1="This is C language";
cout<<"Before replacement,string is"<<str1<<'\n';
str1.replace(8,1,"C##",2); 
cout<<"After replacement,string is"<<str1;
return 0;
}

Output:

Output

Before replacement,string is This is C language
After replacement,string is This is C# language

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience