ERASE()

This method is used to remove characters from a std::string object. This function provides flexibility by allowing the deletion of a single character at a specified position, a contiguous range of characters, or all characters from a given position to the end of the string. It is particularly useful for string manipulation tasks such as cleaning up input, removing unwanted symbols or extra spaces, and adjusting formatting. By combining erase() with other string functions, developers can efficiently modify and sanitize text data within their programs.

stringName.erase(position, length);
stringName.erase(position, length);

 * position: index where deletion starts
 * length: number of characters to remove
#include <iostream>     // For input and output
#include <string>
using namespace std;

int main() {
    string message = "###Welcome";   // String with extra symbols
    message.erase(0, 3);             // Remove the first 3 characters
    cout << "Cleaned Message: [" << message << "]" << endl;  // Display result
    return 0;  // End of program
}

Last updated