We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Remove First N Characters from a string in C++

In this article, we will discuss different ways to delete first n characters from a string in C++.


Suppose we have a string,

"workspace"

After removing first 5 characters from it, the final string should be,

Advertisements

"pace"

Let’s discuss different techniques to do this.

Remove first N Characters from a string using erase() in C++

Using Index Positions

In C++, the string class provides a function erase() to delete the characters from string. In one of its overloaded versions it deletes the characters based on index positions. Syntax is as follows,

string &string::erase(size_t pos, size_t n)

Parameters:

  • pos: Index of first character to be deleted (default 0).
  • n: Number of characters to be deleted (default is end of the string).

Returns:

  • It creates a copy of the calling string object. Then deletes n characters starting from index position pos and returns the modified copy of the string.

We can use this to delete first N characters from string. For that we need to pass the start index position 0 and length N as arguments to erase() function. For example,

#include <iostream>

int main()
{
    std::string sampleStr = "workspace";

    int n = 5;
    
    // Remove first N character from string
    sampleStr.erase(0, n);

    std::cout<<sampleStr << std::endl;

    return 0;
}

Output:

pace

It deleted first 5 characters from string.

Using Iterators

One of the overloaded versions of string::erase() functions accepts two iterators as arguments and deletes the sequence of characters in the range [first,last).

iterator string::erase(iterator first, iterator last);

We can also use an another overloaded version of erase() to delete first n characters from a string. For that we need to pass following arguments,.

  • Iterator pointing to first character of string
  • Iterator pointing to nth character of string

Let’s see how to delete first 5 characters from a string using this technique,

#include <iostream>
#include <iterator>

int main()
{
    std::string sampleStr = "workspace";

    int n = 5;

    // Remove first N character from string
    sampleStr.erase(sampleStr.begin(),
                    sampleStr.begin() + n);
    
    std::cout<<sampleStr << std::endl;

    return 0;
}

Output:

pace

It deleted first 5 characters from string.

Remove First N Characters from a string using substr() in C++

In C++, string class provides a function substr() to get a copy of sub-string from the calling string object,

string substr (size_t pos = 0, size_t len = npos) const;

Parameters:

  • pos : Position of the first character to be copied as a substring. As indexing starts from 0 in C++, so the index position of first character is 0. character is denoted by a value of 0 (not 1).
  • len : Number of characters to be include in the substring from the starting position “pos”. Default value is string::npos, which indicates all characters until the end of the string.

Returns:

  • It returns the sub-string as a newly constructed string object. This sub-string starts at character position pos and spans to len characters or till end of string if len is not provided.

We can use this substr() function to delete first N characters from string. For that we need to pass the number of characters to be deleted as argument to the substr() function. It will consider that as starting index position and will return a new string containing characters from that position till end. We can assign that new string to original variable. It will given an effect taht we have deleted the first N characters from string. For example,

#include <iostream>
#include <iterator>

int main()
{
    std::string sampleStr = "workspace";

    int n = 5;

    // Remove first N character from string
    sampleStr = sampleStr.substr(n);
    
    std::cout<<sampleStr << std::endl;

    return 0;
}

Output:

pace

It deleted first 5 characters from string.

Summary:

We learned about three different ways to delete first N characters from string in C++.

Cart

Your Cart is Empty

Back To Shop