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:
W
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++.