This article will discuss different ways to delete the first character from a string in C++.
Suppose we have a string like this,
"workspace"
We need to delete the first character from this string. The final string should be like this.
"orkspace"
Let’s see how to do that.
Remove the first character from the string using erase()
Using Index Position
In C++, the string class provides a function erase(), and in one of its overloaded versions, it takes two parameters i.e.
string& erase(size_t pos = 0, size_t len = npos);
Parameters:
- pos: An index position.
- len: The number of elements to delete.
It deletes the specified number (len) of characters from the string, starting from the given index position (pos).
Frequently Asked:
- How to Split a String into a vector in C++?
- How to trim strings in C++ using Boost String Algorithm Library
- Convert string to const char* in C++
- How to concatenate a string and int in C++?
We can use this to delete the first character from the string by passing the starting index position as 0 and 1 as the number of elements to be deleted. For example,
#include <iostream> int main() { std::string sampleStr = "workspace"; // Remove first character from string sampleStr.erase(0, 1); std::cout<<sampleStr << std::endl; return 0; }
Output:
orkspace
It deleted the first character from the string in C++.
Using Iterators
There is another overloaded version of the erase() function, i.e., erase(iterator p). It takes an iterator as an argument and deletes the character pointed by the given iterator. We can use this to remove the first character from the string by passing the iterator pointing to 1st character of the string. For example,
#include <iostream> int main() { std::string sampleStr = "workspace"; // Remove first character from string sampleStr.erase(sampleStr.begin()); std::cout<<sampleStr << std::endl; return 0; }
Output:
orkspace
It deleted the first character from the string in C++.
Remove the first character from the string using substr()
In C++, the string class provides a function substr(). It returns a copy of the sub-string selected based on index positions.
Syntax of string::substr()
string substr(size_t pos = 0, size_t len = npos) const;
It accepts two parameters,
- pos: The starting index position (pos). The Position from which it will start copying the characters.
- len: The Number of characters to be selected in the sub-string.
Returns:
- It returns a newly constructed string containing the specified characters from the calling string object.
We can use this to remove the first character from the string. We need to pass the 1 as the argument in the substr() function. It will return a copy of the substring containing the characters from index position 1 to last. For example,
#include <iostream> int main() { std::string sampleStr = "workspace"; // Remove first character from string sampleStr = sampleStr.substr(1); std::cout<<sampleStr << std::endl; return 0; }
Output:
orkspace
It deleted the first character from the string in C++.
Summary:
We learned three different ways to delete the first character from a string in C++.