Remove Last N Characters from a string in C++

This article will discuss different ways to delete last n characters from a string in C++.

Suppose we have a string,

"workspace"

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

work

Let’s discuss different techniques to do this.

Remove last 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 the string. One of its overloaded versions deletes the characters based on index positions. The syntax is as follows,

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

Parameters:

  • pos: Index of the last character to be deleted (default 0).
  • n: Number of characters to be deleted (default is the 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 the last N characters from the string. For that, we need to pass the (size_of_string – n ) as an argument to erase() function. It will deleted the characters from this index position till end. For example,

#include <iostream>
#include <iterator>

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

    int n = 5;

    // Remove last N characters from string
    sampleStr.erase(sampleStr.size() - n);

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

    return 0;
}

Output:

work

It deleted last five characters from the 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 [last,last).

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

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

  • Iterator pointing to the fifth last character of the string
  • Iterator pointing to the end of string

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

#include <iostream>
#include <iterator>

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

    int n = 5;

    // Remove last N characters from string
    sampleStr.erase(sampleStr.end() - n,
                    sampleStr.end() );

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

    return 0;
}

Output:

work

It deleted last 5 characters from string.

Remove last 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 last character to be copied as a substring. As indexing starts from 0 in C++, so the index position of the last character is 0. character is denoted by a value of 0 (not 1).
  • len : Number of characters to be included in the substring from the starting position “pos”. The default value is string::npos, which indicates all characters until the end of the string.

Returns:

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

We can use this substr() function to delete the last N characters from a string. For that, we need to pass the following arguments,

  • 0 as the start index position
  • (size_of_string – n ) as the number of characters to be copied.

It will return a substring. This substring will contain characters from index position 0 till (size_of_string – n ) from the calling string object. We can assign that new string to the original variable. It will give an effect that we have deleted the last N characters from a string. For example,

#include <iostream>
#include <iterator>

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

    int n = 5;

    // Remove last N characters from string
    sampleStr = sampleStr.substr(0, sampleStr.size() - n);

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

    return 0;
}

Output:

work

It deleted the last five characters from a string.

Summary:

We learned three different ways to delete the last N characters from string in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top