Remove First Character from String in C++

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.

Advertisements
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).

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++.

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top