std::string::replace() in C++
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.
Syntax
The 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 Indexes
We can use to replace some part of string with another string or any other character by specifying the 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.
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
- 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.
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
- 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.
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 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.
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
- 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.
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
- 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.
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;
}
Similar Reads
std::string::resize() in C++ resize() lets you change the number of characters. Here we will describe two syntaxes supported by std::string::resize() in C++ Return Value : None Syntax 1: Resize the number of characters of *this to num. void string ::resize (size_type num) num: New string length, expressed in number of character
3 min read
std::string::insert() in C++ In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou
4 min read
std::string::push_back() in C++ The std::string::push_back() method in C++ is used to append a single character at the end of string. It is the member function of std::string class defined inside <string> header file. In this article, we will learn about std::string::push_back() method in C++.Example:C++// C++ Program to ill
2 min read
std::string::data() in C++ The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no '\0' character gets appended at the end of array. Syntax: const char* data() const; char* is the po
2 min read
std::string::compare() in C++ The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file. In this article, we will learn how to use string::compare() in C++.The different ways to
4 min read
std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.String vs Character ArrayStringChar
8 min read
std::string::clear in C++ The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters. Parameters: none Return Value: none void string ::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing CPP //
1 min read
std::string::assign() in C++ The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents. Syntax 1: Assign the value of string str. string& string::assign (const string& str) str : is the string to be assigned. Returns : *this CPP // CPP code for assign
5 min read
std::string::append() in C++ The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class .The string::append() function can be used to do the following append operations:Table of ContentAppend a Whole StringAppend a Part of the StringAppend a C
3 min read
std::replace and std::replace_if in C++ std::replace Assigns new_value to all the elements in the range [first, last) that compare to old_value. The function use operator == to compare the individual elements to old_value Function Template : void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& ne
4 min read