12

Remove First Character from String in C++

 2 years ago
source link: https://thispointer.com/remove-first-character-from-string-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

This article will discuss different ways to delete the first character from a string in C++.

Suppose we have a string like this,

"workspace"
"workspace"

We need to delete the first character from this string. The final string should be like this.

"orkspace"
"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);
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;
#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
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;
#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
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;
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;
#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
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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK