The string::replace() function in C++ is used to replace a single or multiple characters from a given index. It is the member function of std::string class. In this article, we will learn how to use the string::replace() function in our C++ program.
std::string::replace() provides multiple overloads depending on whether the replacement is done using indexes or iterators.
| Syntax | Description |
|---|---|
| str.replace(pos, count, n, ch) | Replaces count characters starting at pos with n copies of ch. |
| str.replace(pos, count, str2) | Replaces count characters starting at pos with str2. |
| str.replace(pos1, count, str2, pos2, len) | Replaces count characters with a substring of str2 starting at pos2 of length len. |
| str.replace(first, last, n, ch) | Replaces the range [first, last) with n copies of ch. |
| str.replace(first, last, str2) | Replaces the range [first, last) with str2. |
| str.replace(first, last, str2_first, str2_last) | Replaces [first, last) with characters in [str2_first, str2_last). |
Replace Using Indexes
Replace with Single Repeated Character
The string::replace() method can be used to replace the multiple characters with single repeated character.
Syntax
str1.replace(pos, n, m, c)
Parameters
- str1:Â String in which we have to replace the multiple characters.
- pos:Â Index to the position in str1 where we have to start replacing the characters.
- n:Â Number of characters which we have to replace.
- m:Â Number of times we have to repeat the single character.
- c:Â Character by which we have to replace.
Return Value: It returns the original string after replacing the multiple characters with single repeated character.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hey World";
// Replace first 3 characters with '!!!'
str.replace(0, 3, 3, '!');
cout << str;
}
Output
!!! World
Replace with Another String
Syntax
str1.replace(pos, n, str2)
Parameters
- str1:Â String in which we have to replace.
- pos:Â Index to the position in str1 where we have to start replacing the characters.
- n:Â Number of characters which we have to replace.
- str2:Â String by which we have to replace the characters.
Return Value: It returns the original string after replacing the multiple characters by another string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hey World";
string str2 = "Hello";
// Replace first 3 characters with str2
str1.replace(0, 3, str2);
cout << str1;
}
Output
Hello World
Replace with Substring
The string::replace() method can also be used to replace the multiple characters with a part of the given string.
Syntax
str1.replace(pos1, n, str2, pos2,m)
Parameters
- str1:Â The string in which we have to replace.
- pos1:Â Index to the position in str1 where we have to start replacing the characters.
- n:Â Number of characters which we have to replace.
- str2:Â String by which we have to replace the characters.
- pos2:Â Starting index of substring which we have to replace with multiple characters.
- m:Â Numbers of the character in the substring by which we have to replace.
Return Value: It returns the original string after replacing the multiple characters by another substring.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hello Geeks";
string str2 = "Hey World";
// Replace "Geeks" with substring "World"
str1.replace(6, 5, str2, 4, 5);
cout << str1;
}
Output
Hello World
Replace Using Iterator
Replace with Repeated Character
The string::replace() method can be used to replace the multiple characters with a single repeated character.
Syntax
str1.replace(first, last, n, c);
Parameters
- str1:Â The string in which we have to replace the multiple characters.
- first:Â Iterator pointing to the starting position of str1 from where we have to replace the multiple characters.
- last:Â Iterator pointing to the position just after the last element up to which we have to replace.
- n:Â Number of single repeated character by which we have to replace.
- c:Â Character by which we have to replace.
Return Value: It return the original string after replacing the multiple characters with single repeated character.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hey World";
auto first = str.begin();
auto last = str.begin() + 3;
// Replace first 3 characters with '!!!'
str.replace(first, last, 3, '!');
cout << str;
}
Output
!!! World
Replace with Another String
The string::replace() method can also be used to replace the multiple characters with a string.
Syntax
str1.replace(first, last, str2)
Parameters
- str1:Â The string in which we have to replace the multiple characters.
- first:Â Iterator pointing to the starting position of str1 from where we have to replace the multiple characters.
- last:Â Iterator pointing to the position just after the last element up to which we have to replace.
- str2:Â String by which we have to replace the characters.
Return Value: It return the original string after replacing the multiple characters by another string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hey World";
string str2 = "Hello World";
str1.replace(str1.begin(), str1.end(), str2);
cout << str1;
}
Output
Hello World
Replace with Substring (Iterator Range)
The string::replace() method can also be used to replace the multiple characters with a substring.
Syntax
str1.replace(first, last, str2_first, str2_last)
Parameters
- str1:Â The string in which we have to replace the multiple characters.
- first:Â Iterator pointing to the starting position of str1 from where we have to replace.
- last:Â Iterator pointing to the position just after the last element up to which we have to replace.
- str2_first:Â Iterator pointing to the starting position of substring by which we have to replace.
- str2_last:Â Iterator pointing to the position just after the last element of the substring.
Return Value: It returns the original string after replacing the multiple characters by another substring.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hello Geeks";
string str2 = "Hey World";
auto first = str1.begin() + 6;
auto last = str1.end();
auto str2_first = str2.begin() + 4;
auto str2_last = str2.end();
str1.replace(first, last, str2_first, str2_last);
cout << str1;
}
Output
Hello World