Example 1: Remove the First Character From the String Using erase() Method in C++
The string::erase function is the recommended method for removing characters from a string in place. Scope overload is demonstrated in the following C++ program:
Let’s start with the program’s main function. Inside the main function parentheses, we have defined a standard class template to represent the string variable as “StrValue”. The string variable “StrValue” is initialized with the string of words. We have a standard cout statement that will print the string as it is initialized. Then, we performed the erase method on this string, “StrValue”.
Within the erase method, we have passed 0 and 1 as an argument. The value “0” is the index of the first character in the string “StrValue”, and the value “1” is the first character placed in the string. After the specified location, the erase method will delete the provided length of the character from the string. In the end, we have a print statement to print the string after removing the first character.
#include <string>
int main()
{
std::string StrValue = "Happy Me";
std::cout << "String Before:" << StrValue << std::endl;
StrValue.erase(0, 1);
std::cout << "String After:" << StrValue << std::endl;
return 0;
}
The following output shows the working of the erasing method over a specified string. As you can see, the string’s initial character has been erased:
Example 2: Remove the First Character From the String Using the Iterator Method in C++
Another overloaded alternative of the erase() method erases the iterator. It accepts an iterator as a parameter and erases the character specified by it. Bypassing the iterator pointing to the string’s first character, we can erase the string’s first character.
We have used erase method in the main function of this following program. A standard format of the string has been defined in the main function. The string variable is created as “StringIs” and initialized with the strings of characters. First, we have printed the string before applying the erase iterator method.
Then, we have invoked the erase method of the previous string. The erase method has the begin iterator for the “StrValue” string and is used to return an iterator that points to the string’s first character. When the first character is returned, the erase method will remove the character from the string. After that, the string will be printed, having the first character removed.
#include <string>
int main()
{
std::string StringIs = "Programming Example";
std::cout << "String Before: " << StringIs << std::endl;
StringIs.erase(StringIs.begin());
std::cout << "String After: " << StringIs << std::endl;
return 0;
}
The shell has displayed the output, which shows the string before the erase iterator method and after the first character is removed from the string.
Example 3: Check the Empty String To Remove the First Character From the String in C++
Before using the string::erase function, ensure there isn’t an empty string. If the input sequence is empty, the program throws a std::length error exception.
The program includes the main function where the string variable “MyStr” is declared. The string is also initialized with string characters when it is declared. The string value will be printed on the shell. After that, we have an if statement where the condition is applied on the given string.
We have called the empty method inside the if condition to check that the string should not be empty. Then, the erase iterator method is used over the string and removes the first character from the specified string. The string will be printed on the shell screen with the first character removed from the string.
#include <string>
int main()
{
std::string MyStr = "rainbow";
std::cout << "String Before:" << MyStr << std::endl;
if (!MyStr.empty()) {
MyStr.erase(MyStr.begin());
}
std::cout << "String Now:" << MyStr << std::endl;
return 0;
}
We have got the following output strings.
Example 4: Remove the First Character With the Matching Character in C++
Now, we have an example to remove the initial character if it corresponds to a specific character, using the following syntax:
The string variable is initialized with the string value and assigned a name “Name” in the following program’s main. We have displayed the string with the cout command. Then, we have defined another variable, “ch”, with the char data type. The char variable “ch” is assigned a character “k” matched with the first character of the previous string.
Then, the if statement is used to check the condition that the character value “k” matched with the first character value of the string. We have the front() function, which is used to get the first character in the string’s reference. The string’s initial character will then be eliminated using the erase iterator.
#include <string>
int main()
{
std::string Name = "Kalsoom";
std::cout << "Name Before: " << Name << std::endl;
char ch = 'K';
if (Name.front() == ch) {
Name.erase(Name.begin());
}
std::cout << "Name Now: " << Name << std::endl;
return 0;
}
As you can see, the first character removed from the string is as follows:
Example 5: Remove the First Character From the String Using the substr() Method in C++
The string is modified using the string::erase method in place. Using the string::substr function, you can retrieve a duplicate of the string without the last character.
We have used the substr function over the string “MyString” in the if condition block. The substr function has passed with the first character position and the length of the string to -1. It returns a newly formed string from the caller string object that contains the specified characters.
#include <string>
int main()
{
std::string MyString = "Bringing";
std::cout << "Original String: "<< MyString << std::endl;
std::string n;
if (!MyString.empty()) {
n = MyString.substr(1, MyString.size() - 1);
}
std::cout << "Changed String: " << n << std::endl;
return 0;
}
The substr method removed the first occurrence of the character from the string, as shown in the following shell:
Conclusion
In C++, we learned the alternative methods for removing the initial character from a string. These methods return the same output but implement differently in C++ programs. These are efficient C++ methods for deleting the initial instances of a character from a string. We hope that these methods will be beneficial to you.