How to Erase String in C++

Jinku Hu Mar 13, 2025 C++ C++ String
  1. Understanding std::string::erase
  2. Erasing a Single Character
  3. Erasing a Range of Characters
  4. Erasing a Substring
  5. Summary of Erase Functionality
  6. Conclusion
  7. FAQ
How to Erase String in C++

When it comes to manipulating strings in C++, the std::string class provides a wealth of options. One of the most useful functions available is std::string::erase, which allows you to remove characters from a string efficiently. Whether you’re cleaning up user input, formatting data, or simply trying to modify a string for better readability, understanding how to use the erase function can significantly enhance your programming skills.

In this article, we’ll dive into the specifics of how to utilize the std::string::erase function in C++. We’ll explore various methods of erasing characters or substrings from a string, providing you with clear code examples and explanations. By the end, you’ll be well-equipped to handle string manipulation tasks in your C++ projects with ease.

Understanding std::string::erase

The std::string::erase function is a member of the std::string class and can be used in multiple ways. It can remove a single character, a range of characters, or even an entire substring. The flexibility of this function makes it an essential tool for any C++ programmer. Let’s take a closer look at how to use this function effectively.

Erasing a Single Character

To erase a single character from a string, you can use the erase function with the index of the character you wish to remove. Here’s an example:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello World!";
    str.erase(5, 1); // Erasing the space character at index 5
    std::cout << str << std::endl;
    return 0;
}

Output:

HelloWorld!

In this code snippet, we first define a string str containing “Hello World!”. By calling str.erase(5, 1), we remove one character starting from index 5, which is the space. The resulting string is “HelloWorld!”, demonstrating how easy it is to manipulate strings in C++.

Erasing a Range of Characters

If you need to remove a range of characters from a string, the erase function can also handle that. You specify the starting index and the number of characters to erase. Here’s how it works:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello World!";
    str.erase(5, 6); // Erasing from index 5 to 10 (6 characters)
    std::cout << str << std::endl;
    return 0;
}

Output:

Hello!

In this example, we again start with the string “Hello World!”. By calling str.erase(5, 6), we remove six characters starting from index 5, which includes the space and the word “World”. The final output is “Hello!”, showcasing the effectiveness of the erase function for removing multiple characters at once.

Erasing a Substring

Sometimes, you may want to erase a specific substring from a string. While the erase function does not directly accept substrings, you can find the substring’s starting index using the find method and then use erase to remove it. Here’s an example:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello World!";
    std::string toErase = "World";
    size_t pos = str.find(toErase);
    
    if (pos != std::string::npos) {
        str.erase(pos, toErase.length());
    }
    
    std::cout << str << std::endl;
    return 0;
}

Output:

Hello !

In this code, we define a string str and a substring toErase. We use str.find(toErase) to locate the starting index of “World”. If the substring is found (i.e., pos is not std::string::npos), we call str.erase(pos, toErase.length()) to remove it. The result is “Hello !”, effectively demonstrating how to erase a specific substring from a string.

Summary of Erase Functionality

The versatility of the std::string::erase function makes it a powerful tool for string manipulation in C++. Whether you’re erasing a single character, a range of characters, or even specific substrings, mastering this function will greatly enhance your ability to handle text data. Remember to always check if the substring exists before attempting to erase it to avoid unexpected behavior.

Conclusion

In summary, the std::string::erase function is an essential feature of C++ that allows for effective string manipulation. By understanding its various applications, you can easily remove characters or substrings from strings, making your code cleaner and more efficient. Whether you’re a beginner or an experienced programmer, mastering this function will undoubtedly improve your C++ programming skills. So, the next time you need to modify a string, remember the power of std::string::erase.

FAQ

  1. What is std::string::erase used for?
    std::string::erase is used to remove characters or substrings from a string in C++.

  2. Can I erase multiple characters at once?
    Yes, you can specify a starting index and the number of characters to erase using std::string::erase.

  3. How do I erase a specific substring?
    You can find the starting index of the substring using std::string::find and then use std::string::erase to remove it.

  4. What happens if I try to erase a character at an invalid index?
    If you attempt to erase a character at an invalid index, it may result in undefined behavior or a runtime error.

  5. Is std::string::erase part of the C++ standard library?
    Yes, std::string::erase is a member function of the std::string class, which is part of the C++ standard library.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ String