Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++

What is string erase in C++?

Sarvech Qadir

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 C++ string library allows you to erase a part of a string using the erase() function.

The function works differently according to the parameters passed.

1. erase()

erase() will erase the complete string. Here is an executable example:

#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
cout << "Initially: " << str << endl;
str.erase();
cout << "After using erase(): " << str;
return 0;
}

2. Using erase(position)

erase(position) will delete all the characters after the specified position. Take a look at the code below:

#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
cout << "Initially: " << str << endl;
str.erase(2);
cout << "After using erase(2): " << str;
return 0;
}

3. Using erase(index, length)

erase(index, length) will delete the specified number (length) of characters after the specified position. Take a look at the code below:

#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
cout << "Initially: " << str << endl;
str.erase(2, 4);
cout << "After using erase(2, 4): " << str;
return 0;
}

4. Using erase(iterator index)

erase(iterator index) will delete the specific character at the specified iterator position. Here is an executable example for you to try:

#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
cout << "Initially: " << str << endl;
str.erase(str.begin() + 2);
cout << "After using str.erase(str.begin() + 2): " << str;
return 0;
}

5. Using erase(iterator begin, iterator end)

erase(iterator begin, iterator end) will delete specific characters, starting from the iterator begin position before the iterator end position. It does not delete the character at iterator end. Take a look at the following code:

#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
cout << "Initially: " << str << endl;
str.erase(str.begin() + 2, str.begin() + 5);
cout << "After using erase(str.begin() + 2, str.begin() + 5) " << str;
return 0;
}

RELATED TAGS

c++

CONTRIBUTOR

Sarvech Qadir
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