c++ string.erase
时间: 2023-09-03 19:09:32 浏览: 117
The `string.erase()` function is a member function of the C++ standard library's `string` class. It is used to remove a portion of the string, starting at a specified position and for a specified length.
Syntax:
```
string.erase(position, length);
```
Here, `position` is an integer value that specifies the starting position from where the characters are to be removed. `length` is also an integer value that specifies the number of characters to be removed.
Example:
```c++
#include <iostream>
#include <string>
int main()
{
std::string str = "Hello, World!";
// remove the first 6 characters
str.erase(0, 6);
std::cout << str << std::endl; // output: World!
return 0;
}
```
In the above example, the `erase()` function is used to remove the first 6 characters from the string `str`. The resulting string is then output to the console.
阅读全文