C++ std::string replace() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::replace() removes a slice of characters and inserts new text in that place.

Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Index form is common:

C++
s.replace(pos, len, newText);
s.replace(pos, len, n, ch);
CallWhat it does
s.replace(pos, len, str)Replace len chars at pos with str.
s.replace(pos, len, n, ch)Replace that slice with n copies of ch.

If pos > size(), throws std::out_of_range. If len is large, it replaces through the end.

Library declarations
C++
string& replace(size_type pos, size_type len, const string& str);
string& replace(size_type pos, size_type len, size_type n, char c);

Parameters

ParameterDescription
pos, lenSlice to remove.
replacementString, C-string, or repeated character.

Return Value

Return ValueDescription
string&Reference to *this.

C++ Examples

Example 1 — Basic replace()

Replace a middle word.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "I like Java";
    s.replace(7, 4, "C++");
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Index 7 length 4 selects "Java".
  2. It is replaced with "C++".
  3. Surrounding text stays.

Example 2 — Replace With Stars

Mask part of a string.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "secret";
    s.replace(0, 3, 3, '*');
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. First three letters are removed.
  2. Three stars are inserted.
  3. Suffix "ret" remains.

Example 3 — Replacement Can Grow

New text can be longer than the slice.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "aXb";
    s.replace(1, 1, "YYY");
    std::cout << s << '\\n';
    std::cout << s.size() << '\\n';
    return 0;
}

How It Works

  1. One character 'X' is removed.
  2. Three Ys take its place.
  3. Size grows from 3 to 5.

Frequently Asked Questions

It removes a slice and inserts new characters there.
Yes. The string grows or shrinks as needed.
Throws std::out_of_range when pos > size().
Use assign or operator=; or replace(0, s.size(), newText).
C++23 overload that inserts a range as the replacement.

Next: replace_with_range()

Keep going through the string functions. Replace an iterator range with another range (C++23).

replace_with_range() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful