Updated April 20, 2023
Introduction to C++ replace()
The string library has many functions in C++. The replace() function is a part of the string functions which C++ provides. It helps in replacing a part of the string which will begin with a certain position which will act as a start and it will extend till a certain number of characters. This string will be replaced with the specified string. The string to be replaced with the start and end positions should be given to the replace() function as parameters that help us in replacing the string. Let us check in detail how this function works and how we can use it in different examples.
Syntax:
string1.replace(start,length,string2);
Let us check the below parameters and what is their significance.
- string1: This is a string object. This object is the value that can be copied into another string object.
- start: The start specifies the position from which the replace has to work. The character from which the string should be replaced.
- Length: the length parameter defines the number of characters that will be replaced from the given string.
- String2: this will be the string that you would like to replace the string1. It will be the new string which will replace the old one.
How does C++ replace() Work?
We have checked the parameters required for replace() function. Let us now check how this function works step by step.
Code:
#include<iostream>
using namespace std;
int main()
{
string string1 = "Let us learn C language";
string string2 = "C++";
cout << "The string before replacement is: "<<string1<<'\n';
string1.replace(13,1,string2);
cout << "The string after replacement is: "<<string1<<'\n';
return 0;
}
The function you checked above will now display how it actually works. The main function here has a string where it says ‘Let us learn C language’. Then the programmer realized that we are learning C++.Hence we will use the replace() function here which will help us in correcting the string1. We have given the start position here are 14. It is the position of C. This character is 1 hence the length is defined as 1 for the length parameter. The third parameter is string2 that is ‘C++’ which needs to be replaced with C here. Once this is done the function will work according to its functionality and give us the required output. It will print the output as Let us learn the C++ language. We will check more examples in the coming section which will help you understand this functionality better.
Examples of C++ replace()
Following are the examples given below:
Example #1
Code:
#include <iostream>
using namespace std;
// Function for demonstration
void letsreplace(string str1, string str2, string str3, string str4)
{
// The first 8 characters will be replaced starting from 30 position
str1.replace(30, 8, str2);
cout << str1 << endl;
// Thestr four will have a string appended at the beginning
str4.replace(5, 0, " Hey, how are you ");
cout << str4 << endl;
// It will start replacing at the beginning and end
str4.replace(5, 5, str3, 0, 11);
cout << str4 << endl;
// It will repalce 1 character at 14 and append exclamation after it
str4.replace(14, 1, 3, '!');
cout << str4 << endl;
}
// Main code
int main()
{
string str1 = "This is an example of replace function in C++";
string str2 = "Example";
string str3 = "EduCBAians ";
string str4 = "Hello EduCBAians !";
letsreplace(str1, str2, str3, str4);
return 0;
}
Output:
The above example has 3 variations of the replace function. Let us check the working of each of these ones by one. We have declared 4 strings that are being used in the let’s replace function. This is a user-defined function where we are using the replace function in order to replace the given strings. In the first replace we are using the str1 where we are starting with position 30 and replace a length of 8 characters by str2. The second variant has is starting the replace from 3rd position and going till the fifth position. It is replacing with str4. The next one replaces 5 characters starting from 4th position. In addition to this, it also replaced by 5 characters from the 0th of string3. The last example replaces from 14th character and also adds 3 exclamation marks. This is illustrious and you can check the below output in order to understand these examples better. Below will be the output of the above code.
Example #2
Replace a special character with a string.
Code:
#include <iostream>
#include <string>
int main()
{
std::string str = "Edu*";
std::string rep = "*", y = "CBA";
size_t position;
while ((position = str.find(rep)) != std::string::npos) {
str.replace(position, 1, y);
}
std::cout << "The place to learn new things is " << str;
return 0;
}
Output:
The above code is an example where we are replacing a character with a string. We have taken string str which is Edu*. The * we will be replaced with another string which is defined. We have also taken a position variable that will define the start of the string. We have used a while statement that goes until it reaches the * in the string. It compares it with the string until it is not equal. The place where * starts and ends with the replacement string which is already specified. This is then given as an output string. The following will be the output of the above program. It replaces the original string with the replacement string which is specified.
Conclusion
The replace() function is a function which helps us in replacing certain strings. It is an easy and efficient function that takes the start position, length and string to be replaced with as arguments and then provides the results accordingly. The replace function is fast and also enhances the performance of the code generated. In addition to this, you can also add extra string by using this function in an optimizing manner. There is an advanced version that uses boost and can help in masking the data effortlessly.
Recommended Articles
This is a guide to C++ replace(). Here we also discuss the definition and how does c++ replace() work? along with different examples and its code implementation. You may also have a look at the following articles to learn more –