How to Remove a Character from String in C++

In this article, we will discuss different ways to remove all occurrences of a character from a string in C++.

Table of Contents

Remove a Character from String using std::remove() & string::erase()

In C++, a string is a collection of characters, and it has all the features of a container. Therefore, STL Algorithms built for containers can work for strings too.

The Standard Template Library provides a function std::remove() i.e.

template <class ForwardIterator, class T>
  ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);

It accepts a range (first & last iterator of a container) and a value as arguments. Then it shifts all the occurrences of the given element to the end of the container and returns an iterator pointing to the new end i.e., the start of the segment where all matching elements are shifted.

We can use the remove() function to shift all occurrences of a given character to the end of the string. Then using the string::erase() function, we can delete this segment from the end of the string. For that, we need to pass two arguments to the string::erase() function,

  1. An iterator returned by remove() function
  2. An iterator pointing to the end of the string.

The string::erase() function will remove the characters in the provided range. Let’s learn a little bit more about the string::erase() function,

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

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

So, to delete all occurrences of a character from a string, we can call the remove() function to shift the matching characters to the end of the string. Then call the string::erase() function to delete that part of the string.

Let’s use the remove() and string::erase() function to remove all occurrences of a character from string. For example,

#include <iostream>
#include <algorithm>

int main()
{
    std::string strValue = "Today is the last monday";

    // Character to be deleted from string
    char ch = 'a';

    // Remove all occurrences of a character from string
    strValue.erase(std::remove( strValue.begin(),
                                strValue.end(),
                                ch),
                    strValue.end());

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

    return 0;
}

Output:

Tody is the lst mondy

It removed all the occurrences of character ‘a’ from the string.

Remove a Character from String using std::erase() in C++20

The C++20 introduced a new STL Algorithm, std::erase(container, element), to delete all occurrences of an element from a container. It accepts two arguments,

  1. An STL Container from which we need to delete elements
  2. Value of the element to be deleted

It deletes all occurrences of a given element from the container.

To remove all occurrences of a character from string, just pass the string and the character to be deleted as arguments in the std::erase() function. For example,

#include <iostream>
#include <algorithm>

int main()
{
    std::string strValue = "Today is the last monday";

    // Character to be deleted from string
    char ch = 'a';

    // Remove all occurrences of a character from string
    std::erase(strValue, ch);

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

    return 0;
}

Output:

Tody is the lst mondy

It removed all the occurrences of character ‘a’ from the string.

To compile this code use this command,

g++ --std=c++2a temp.cpp

Remove a Character from String using Regex

In C++, the regex provides a function regex_replace() to replace the contents in string. In one of its overloaded version, it accepts three arguments,

string regex_replace (string, regex_pattern, replacement_string)

Parameters

  • String from which we need to replace the characters.
  • The regex pattern is based on which sub-strings will be matched in the string.
  • The replacement string that will replace the matched substrings.

It returns a new string object with the replaced contents.

We can use the regex_replace() function to remove all occurrences of a character from string. For that we need to pass these three arguments to the regex_replace() function,

  1. String from which we need to remove characters
  2. A regex pattern that matches all the occurrences of given character.
  3. An empty string as the replacement string.

It will replace the occurrences of matched characters with empty string. Basically it will delete all occurrences of the matched character from the string. For example,

#include <iostream>
#include <regex>

int main()
{
    std::string strValue = "Today is the last monday";

    // Character to be deleted from string
    char ch = 'a';

    // Remove all occurrences of a character from string
    strValue = std::regex_replace( strValue,
                                   std::regex("a"),
                                   "");

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

    return 0;
}

Output:

Tody is the lst mondy

It removed all the occurrences of character ‘a’ from the string.

Summary

We learned about three different ways to remove a character from a 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