std:🧵:replace() in C++ (original) (raw)
The std:🧵:replace() function in C++ is used to replace characters or substrings inside a string. It is a member function of the std::string class and provides multiple overloads for replacing text using indexes or iterators.
- It can replace characters, substrings, or ranges of characters.
- It supports replacement using indexes as well as iterators. C++ `
#include #include using namespace std;
int main() {
string str = "Hello World";
str.replace(6, 5, "C++");
cout << str;
return 0;}
`
**Explanation:
- Replacement starts from index 6.
- 5 characters (World) are replaced with "C++".
Common Syntax of string::replace()
| Syntax | Description |
|---|---|
| str.replace(pos, count, n, ch) | Replaces count characters starting from pos with n copies of character ch. |
| str.replace(pos, count, str2) | Replaces characters with another string. |
| str.replace(pos1, count, str2, pos2, len) | Replaces characters with a substring of another string. |
| str.replace(first, last, n, ch) | Replaces iterator range with repeated characters. |
| str.replace(first, last, str2) | Replaces iterator range with another string. |
| str.replace(first, last, str2_first, str2_last) | Replaces iterator range with another iterator range. |
Replace Using Indexes
n index-based replacement, positions are specified using integer indexes.
Replace with 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.
C++ `
#include #include using namespace std;
int main() { string str = "Hey World";
// Replace first 3 characters with '!!!'
str.replace(0, 3, 3, '!');
cout << str;}
`
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.
C++ `
#include #include 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;}
`
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.
C++ `
#include #include 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;}
`
Replace Using Iterator
Replace Iterator Range 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.
C++ `
#include #include 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;}
`
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.
C++ `
#include #include using namespace std;
int main() { string str1 = "Hey World"; string str2 = "Hello World";
str1.replace(str1.begin(), str1.end(), str2);
cout << str1;}
`
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.
C++ `
#include #include 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;}
`