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.

#include #include using namespace std;

int main() {

string str = "Hello World";

str.replace(6, 5, "C++");

cout << str;

return 0;

}

`

**Explanation:

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

**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

**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

**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

**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

**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

**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;

}

`