Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

string
replace
function
c++

What is the string replace function in C++?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The replace method of the String class replaces a portion of a​ string that begins at a specific starting position and spans a specific number of characters.

The starting position, the number of characters, and the respective replacement string are all ​passed as arguments.

Syntax

svg viewer

Code

The example below shows how the replace method is used to modify strings:

#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Welcome to Educative!";
// replace "come" of "Welcome" with "abcd".
// starting position = 3, length = 4,
// replacement string = "abcd"
str.replace(3,4,"abcd");
cout << str << endl;
return 0;
}

RELATED TAGS

string
replace
function
c++
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring