C++ string erase() Function
Example
Remove 6 characters, starting at position 5:
string txt = "Hello World!";
txt.erase(5, 6);
cout << txt;
Definition and Usage
The erase() function removes characters from a string. It changes the original string.
If you only give the start position, all characters from that position to the end are removed.
The erase function is defined in the <string> header file.
Syntax
string.erase(size_t start, size_t length);
The size_t data type is a non-negative integer.
Parameter Values
| Parameter | Description |
|---|---|
| start | Optional. The position of the first character to remove. Default is 0. |
| length | Optional. The number of characters to remove. Default is all characters to the end of the string. |
Technical Details
| Returns: | A reference to the changed string. |
|---|
Related Pages
Read more about strings in our String Tutorial.