String erase() in C++
In C++, strings are a sequence of characters represented by the string class. The string class provides many member functions that allow us to manipulate strings in various ways. One of the commonly used member functions is the erase() function, which is used to remove a portion of a string.
In this blog post, we will discuss the erase()function in detail. We will cover its syntax, example usage, and output, among other things.
Syntax:
The syntax of the erase() function is as follows:
string erase (size_t pos, size_tlen = npos);
The function takes two arguments:
- pos: This argument specifies the position from where we want to start erasing characters from the string.
- len: This argument specifies the number of characters we want to erase from the string. If len is not provided, the function will erase all characters from pos to the end of the string.
Example:
Let's consider the following example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Hello, World!";
str.erase(5, 7);
cout<< str <<endl;
return 0;
}
Output:
The output of the above code will be:
Hello!
Explanation:
In the above code, we have created a string str with the value "Hello, World!". After that, we called the erase() function on the string, specifying the position to start erasing from (5) and the number of characters to erase (7). The function removes the characters from position 5 to position 11, leaving us with the string "Hello!".
If we don't provide the len argument, the function will erase all characters from the specified position to the end of the string.
Example:
Let's consider the following code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Hello, World!";
str.erase(7);
cout<< str <<endl;
return 0;
}
Output:
The output of the above code will be:
Hello,
Explanation:
In the above code, we have created a string str with the value "Hello, World!". After that, we called the erase() function on the string, specifying the position to start erasing from (7) but not specifying the number of characters to erase. The function removes all characters from position 7 to the end of the string, leaving us with the string "Hello,".
Multiple Erases
We can call the erase() function multiple times to remove different portions of a string.
Example:
Let's consider the following example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Hello, World!";
str.erase(5, 7);
str.erase(0, 1);
cout<< str <<endl;
return 0;
}
Output:
The output of the above code will be:
ello
Explanation:
In the above code, we have created a string str with the value "Hello, World!". We first called the erase() function to remove the characters from position 5to position 11. After that, we called the function again to remove the character at position 0. The final string value is "ello".
Conclusion
In conclusion, the erase() function is a very useful member function of the string class that allows us to remove portions of a string. It takes two arguments, the position from where we want to start erasing characters and the number of characters we want to erase. If the second argument is not provided, the function will erase all characters from the specified position to the end of the string. We can call the function multiple times to remove different portions of a string.
Moreover, the erase()function can also be used to remove specific characters from a string, rather than a specific range of characters. We can achieve this by using the remove() and erase() functions together. The remove() function will shift all unwanted characters to the end of the string, and then we can use the erase() function to remove them from the end.
It is important to note that the erase() function modifies the original string. If we want to keep the original string intact, we should make a copy of the string and then call the function on the copy.
Overall, the erase() function is a very useful tool in C++ for manipulating strings. By understanding its syntax and example usage, we can effectively use it to modify strings in our programs.