Remove a Character from String by Index in C++

This article will discuss different ways to delete a character from a string by its index position.

A string in C++ is a collection of characters, and indexing in it starts from 0. So, the index position of Nth character in a string is N-1. Like,

  • Index position of 1st character of a string is 0
  • Index position of 2nd character of a string is 1
  • Index position of 3rd character of a string is 2
  • ….
  • ….
  • Index position of Nth character of a string is N-1

Suppose we have a string “workspace” and if we delete the character at index position 3, it will remove the 4th character from the string and the final string should be “worspace”.

There are different ways to delete a character from a string by its index position. Let’s discuss them one by one.

Remove character from string by index position using std::erase()

The string class in C++, provides a function erase() to delete characters from string. In one of the overloaded versions, it accepts two arguments,

string& erase (size_t pos = 0, size_t len = npos);

Parameters:

  • pos: Index position of a character in string, from where we want to start the deletion.
  • len: Number of characters to be deleted from the string

It removes the “len” number of characters from the index position “pos.

We can use this function to delete the nth character from the string. For that, we need to pass the index position as the first argument and 1 as the second argument. For example,

#include <iostream>

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

    int indexPos = 3;

    // Remove character from string at given index position
    strValue.erase(indexPos, 1);

    std::cout<<strValue << std::endl;


    return 0;
}

Best Courses to Learn Modern C++11, C++17 & C++20

Many high-performance applications extensively use C++ like in the financial sector, game development, and many other software where speed and load matter. We have created a list of best C++ courses, that can teach you cutting edge modern C++

Output:

worspace

It deleted the character at index position 3 from the string.

Remove character from string by index position using iterators

The string class in C++, provides a function erase() to delete characters from a string. In one of the overloaded versions, it accepts an iterator range i.e.

iterator erase (iterator first, iterator last);

It deletes the sequence of characters in the range [first,last).

We can use this function to delete the string’s character at nth index position. We need to pass the iterator of nth and (n+1)th character as arguments. For example,

#include <iostream>

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

    int indexPos = 3;

    // Remove character from string at given index position
    strValue.erase( strValue.begin() + indexPos,
                    strValue.begin() + indexPos + 1);

    std::cout<<strValue << std::endl;


    return 0;
}

Output:

worspace

It deleted the character at index position 3 from the string.

Instead of adding numeric digits to the iterator, we can also use the std::advance() function to get the iterator of nth and (n+1) characters from the string and then delete a character at the index position n from the string. For example,

#include <iostream>
#include <algorithm>

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

    int indexPos = 3;

    auto start = strValue.begin();
    auto end = strValue.begin();
    
    std::advance(start, indexPos);
    std::advance(end, indexPos + 1);
    
    // Remove character from string at given index position
    strValue.erase(start, end);

    std::cout<<strValue << std::endl;


    return 0;
}

Output:

worspace

It deleted the character at index position 3 from the string.

Summary

We learned three different ways to delete a character from a string by index position.

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