std::string::replace() in C++

Last Updated : 13 Mar, 2026

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.

std::string::replace() provides multiple overloads depending on whether the replacement is done using indexes or iterators.

SyntaxDescription
str.replace(pos, count, n, ch)Replaces count characters starting at pos with n copies of ch.
str.replace(pos, count, str2)Replaces count characters starting at pos with str2.
str.replace(pos1, count, str2, pos2, len)Replaces count characters with a substring of str2 starting at pos2 of length len.
str.replace(first, last, n, ch)Replaces the range [first, last) with n copies of ch.
str.replace(first, last, str2)Replaces the range [first, last) with str2.
str.replace(first, last, str2_first, str2_last)Replaces [first, last) with characters in [str2_first, str2_last).

Replace Using 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.

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "Hey World";

    // Replace first 3 characters with '!!!'
    str.replace(0, 3, 3, '!');

    cout << str;
}

Output
!!! World

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 <iostream>
#include <string>
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;
}

Output
Hello World

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 <iostream>
#include <string>
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;
}

Output
Hello World

Replace Using Iterator

Replace 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 <iostream>
#include <string>
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;
}

Output
!!! World

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 <iostream>
#include <string>
using namespace std;

int main() {
    string str1 = "Hey World";
    string str2 = "Hello World";

    str1.replace(str1.begin(), str1.end(), str2);

    cout << str1;
}

Output
Hello World

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 <iostream>
#include <string>
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;
}

Output
Hello World
Comment