Table of contents
1.
Introduction
2.
Parameters  
2.1.
1. Using a Single Position  
2.2.
2. Using a Range of Positions  
2.3.
3. Using Iterators  
3.
Erase Single Character from Given Position
3.1.
Syntax
3.2.
Example
4.
Return Values  
4.1.
1. Return Value for Single Position & Range  
4.2.
2. Return Value for Iterators  
4.3.
3. Handling Edge Cases  
5.
Erase Range of Characters
5.1.
Example
6.
Erase k Characters After Given Position
6.1.
Example
7.
Erase All Characters After Given Position
7.1.
Example
8.
Erase All Characters in a String
8.1.
Example
9.
Frequently Asked Questions
9.1.
What happens if I try to erase characters beyond the string length?
9.2.
Can I use the clear() function instead of erase()?
9.3.
Is erase() the best method for modifying strings?
10.
Conclusion
Last Updated: Mar 3, 2025
Easy

String Erase in C++

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C++, the string erase() function is used to remove characters from a string, either at a specific position or within a given range. This function modifies the original string and adjusts its size accordingly. It is commonly used for trimming, removing unwanted characters, or modifying substrings efficiently.

String Erase in C++

In this article, you will learn about the erase() function in C++, its syntax, different ways to use it, and examples demonstrating its implementation.

Parameters  

The `erase()` function in C++ is used to remove characters from a string. It is part of the `std::string` class & can be used in three different ways depending on the parameters you pass to it. Let’s discuss each of these methods:  

1. Using a Single Position  

You can remove a single character from a string by specifying its position. The syntax for this is:  

string.erase(position);  

 

Here, `position` is the index of the character you want to remove. For example:  

include <iostream>  
include <string>  
using namespace std;  

int main() {  
    string str = "Hello World";  
    str.erase(5); // Removes the character at index 5  
    cout << str;
    return 0;  
}  
You can also try this code with Online C++ Compiler
Run Code


Output: 

HelloWorld 

 

In this example, the space at index 5 is removed, & the string becomes "HelloWorld".  

2. Using a Range of Positions  

You can also remove a range of characters by specifying the starting & ending positions. The syntax is:  

string.erase(start_position, length);  

 

Here, `start_position` is the index where the removal begins, & `length` is the number of characters to remove. For example:  

include <iostream>  
include <string>  
using namespace std;  

int main() {  
    string str = "Hello World";  
    str.erase(5, 6); // Removes 6 characters starting from index 5  
    cout << str;
    return 0;  
}  
You can also try this code with Online C++ Compiler
Run Code

 

Output: 

Hello  

 

In this case, the function removes 6 characters starting from index 5, which deletes " World" & leaves "Hello".  

3. Using Iterators  

The `erase()` function can also work with iterators. You can remove a single character or a range of characters using iterators. The syntax is:  

string.erase(iterator_position);  
string.erase(start_iterator, end_iterator);  

 

For example:  

include <iostream>  
include <string>  
using namespace std;  

int main() {  
    string str = "Hello World";  
    str.erase(str.begin() + 5); // Removes the character at index 5  
    cout << str;   
    return 0;  
}  
You can also try this code with Online C++ Compiler
Run Code

 

Output

HelloWorld  

 

Here, `str.begin() + 5` points to the space character, which is removed.  

Erase Single Character from Given Position

The erase() function allows us to remove a single character from a specified index in a string.

Syntax

string.erase(position, number_of_characters);
  • position: The index from where the deletion begins (0-based index).
     
  • number_of_characters: Number of characters to be deleted (default is 1).

Example

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

int main() {
    string str = "Hello World";
    str.erase(5, 1); // Removes the space at index 5
    cout << "Updated string: " << str << endl;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Updated string: HelloWorld

Return Values  

The `erase()` function in C++ not only modifies the string but also returns a value. Understanding what it returns is important for writing efficient & error-free code. Let’s understand this in detail:  

1. Return Value for Single Position & Range  

When you use the `erase()` function with a single position or a range, it returns the modified string itself. This means you can chain multiple operations together if needed. For example:  

include <iostream>  
include <string>  
using namespace std;  

int main() {  
    string str = "Hello World";  
    str.erase(5, 6).erase(0, 1); // Removes " World" & then "H"  
    cout << str; 
    return 0;  
}  
You can also try this code with Online C++ Compiler
Run Code


Output: 

ello  


In this example, the first `erase()` removes " World", & the second `erase()` removes the first character "H". The final string is "ello".  

2. Return Value for Iterators  

When you use the `erase()` function with iterators, it returns an iterator pointing to the character immediately after the last erased character. This is useful when you’re working with loops or need to continue processing the string after erasing. For example:  

include <iostream>  
include <string>  
using namespace std;  

int main() {  
    string str = "Hello World";  
    auto it = str.erase(str.begin() + 5); // Removes the space & returns iterator  
    cout << it; 
    return 0;  
}  
You can also try this code with Online C++ Compiler
Run Code


Output: 

 

Here, after removing the space, the iterator `it` points to the character 'W', which is the next character in the string.  

3. Handling Edge Cases  

It’s important to handle edge cases when using `erase()`. For example, if you try to erase characters beyond the string’s length, the function will throw an error or produce unexpected results. Always ensure the positions & lengths you provide are within the string’s bounds.  

Erase Range of Characters

We can erase multiple characters by specifying a range.

Example

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

int main() {
    string str = "Programming";
    str.erase(3, 4); // Removes 4 characters starting from index 3
    cout << "Updated string: " << str << endl;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Updated string: Proing

Erase k Characters After Given Position

If we want to erase k characters after a specific position, we can use erase() effectively.

Example

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

int main() {
    string str = "ComputerScience";
    int pos = 3, k = 5;
    str.erase(pos, k); // Removes 5 characters starting from index 3
    cout << "Updated string: " << str << endl;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Updated string: Comence

Erase All Characters After Given Position

To remove all characters after a certain index, we specify the position and delete everything after it.

Example

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

int main() {
    string str = "DataStructures";
    int pos = 4;
    str.erase(pos); // Removes all characters after index 4
    cout << "Updated string: " << str << endl;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Updated string: Data

Erase All Characters in a String

To clear an entire string, we use the erase() function without specifying an index.

Example

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

int main() {
    string str = "Hello, World!";
    str.erase(); // Removes all characters in the string
    cout << "Updated string: " << str << endl;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Updated string:

 

(Note: The output will be an empty string.)

Frequently Asked Questions

What happens if I try to erase characters beyond the string length?

If you specify an index beyond the length of the string, it will cause an out-of-range error or undefined behavior.

Can I use the clear() function instead of erase()?

Yes, clear() is a better alternative for removing all characters from a string as it is more efficient.

Is erase() the best method for modifying strings?

It depends on the use case. If you need to modify a large string frequently, using erase() might not be efficient due to shifting operations.

Conclusion

In this article, we learned how to erase a substring or characters from a string in  C++ using the erase() function of the std::string class. This function allows removing characters by position and length or using iterators. Understanding string manipulation with erase() helps in efficiently modifying and managing text data in  C++.

Live masterclass