Last Updated : 22 Oct, 2024
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.
SyntaxThe string::replace() function provides 6 different overloads for different purposes:
str1.replace(pos, n, m, c) // Replace with character
str1.replace(pos, n, str2) // Replace with string
str1.replace(pos1, n, str2, pos2,m) // Replace with substring
str1.replace(first, last, n, c); // Replace Character
str1.replace(first, last, str2) // Replace String
str1.replace (first, last, str2_first, str2_last); // Replace Substring
These overloads provide different ways to replace characters in a string using string::replace method as given below:
Replace Using IndexesWe can use to replace some part of string with another string or any other character by specifying the indexes.
Replace with Single Repeated CharacterThe string::replace() method can be used to replace the multiple characters with single repeated character.
Syntax
str1.replace(pos, n, m, c)
Parameters
Return Value
Example
C++
// C++ Program to show, how to use
// string::replace() for replacing the
// multiple characters with single repeated
// character
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Hey World";
// Replaces 3 character from 0th index of
// str with 3 copies of '!'
str.replace(0, 3, 3, '!');
cout << str << endl;
return 0;
}
Replace with Another String
The string::replace() method can also be used to replace the multiple characters with a string.
Syntax
str1.replace(pos, n, str2)
Parameters
Return Value
Example
C++
// C++ Program to show, how to use
// string::replace() for replacing the
// multiple characters from another string
#include <bits/stdc++.h>
using namespace std;
int main() {
string str1 = "Hey World";
string str2 = "Hello";
// Replaces 3 characters from 0th
// index by str2
str1.replace(0, 3, str2);
cout << str1 << endl;
return 0;
}
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
Return Value
Example
C++
// C++ Program to show, how to use
// string::replace() for replacing the
// multiple characters from another substring
#include <bits/stdc++.h>
using namespace std;
int main() {
string str1 = "Hello Geeks";
string str2 = "Hey World";
// Replaces 5 characters from 6th index
// of str1 with 5 characters from 4th of str2
str1.replace(6, 5, str2, 4, 5);
cout << str1 << endl;
return 0;
}
Replace Using Iterator
We can use to replace some part of string with another string or any other character by using the iterators.
Replace with Single Repeated CharacterThe string::replace() method can be used to replace the multiple characters with a single repeated character.
Syntax
str1.replace(first, last, n, c);
Parameters
Return Value
Example
C++
// C++ Program to show, how to use
// string::replace() for replacing the
// multiple characters with single repeated
// character
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Hey World";
// Defining the range
auto first = str.begin();
auto last = str.begin() + 3;
// Replaces firts 3 character of
// str with 3 copies of '!'
str.replace(first, last, 3, '!');
cout << str << endl;
return 0;
}
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
Return Value
Example
C++
// C++ Program to show, how to use
// string::replace() for replacing the
// multiple characters from another string
#include <bits/stdc++.h>
using namespace std;
int main() {
string str1 = "Hey World";
string str2 = "Hello World";
// Defining the range
auto first = str1.begin();
auto last = str1.end();
// Replaces the whole string str1 by str2
str1.replace(first, last, str2);
cout << str1 << endl;
return 0;
}
Replace with Another Substring
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
Return Value
Example
C++
// C++ Program to show, how to use
// string::replace() for replacing the
// multiple characters from another substring
#include <bits/stdc++.h>
using namespace std;
int main() {
string str1 = "Hello Geeks";
string str2 = "Hey World";
// Defining the range
auto first = str1.begin() + 6;
auto last = str1.end();
auto str2_first = str2.begin() + 4;
auto str2_last = str2.end();
// Replace the last 5 characters of str1
// by last 5 characters of str2
str1.replace(first, last, str2_first,
str2_last);
cout << str1 << endl;
return 0;
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4