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::size() in C++
The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string.In this article, we will learn about string::size()
2 min read
std::string::insert() in C++
In C++, the string::insert() function is used insert the characters or a string at the given position of the string. It is the member function of std::string class.The string::insert method can be used in the following ways to:Table of ContentInsert a Single CharacterInsert a Single Character Multip
4 min read
std::to_string in C++
In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings.In this article, we will learn how to use std::to_string() in C++.Syntaxstd::to_strin
1 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::basic_string::operator[] in C++
Returns a reference to the character at specified location pos. No bounds checking is performed. If pos > size(), the behavior is undefined. Syntax : reference operator[] (size_type pos); const_reference operator[] (size_type pos) const; Parameters : pos - position of the character to return Retu
1 min read