How to remove Substrings from a String in C++

In this article we will discuss how to remove single or multiple sub strings from a given string.

std::string class provides a member function string::erase() to remove some characters from a given position i.e.

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

It accepts a position and length of characters to be deleted from that position. It removes those characters from string object and also returns the updated string.

Let’s use this string::erase() function to remove sub strings from a string i.e.

Remove First Occurrence of given substring from main string

Let’s remove first occurrence of sub string “this” from string “Hi this is a sample string is for is testing. this is it .”.

/*
 * Erase First Occurrence of given  substring from main string.
 */
void eraseSubStr(std::string & mainStr, const std::string & toErase)
{
	// Search for the substring in string
	size_t pos = mainStr.find(toErase);

	if (pos != std::string::npos)
	{
		// If found then erase it from string
		mainStr.erase(pos, toErase.length());
	}
}

To remove first occurrence of sub string only then we need to search it’s position and use string::erase() to remove it from string i.e.

Remove all Occurrences of given substring from main string.

Let’s remove all occurrences of sub string “is” from string “Hi this is a sample string is for is testing. this is it .”.

/*
 * Erase all Occurrences of given substring from main string.
 */
void eraseAllSubStr(std::string & mainStr, const std::string & toErase)
{
	size_t pos = std::string::npos;

	// Search for the substring in string in a loop untill nothing is found
	while ((pos  = mainStr.find(toErase) )!= std::string::npos)
	{
		// If found then erase it from string
		mainStr.erase(pos, toErase.length());
	}
}

To remove all occurrence of a sub string, we need to search it’s position in loop until it’s not found and for each occurrence we need to use string::erase() to remove it from string.

Erase all Occurrences of all given sub strings from main string using C++11

Let’s remove all occurrences of 3 sub strings i.e. “for”, “is” and “testing” from string “Hi this is a sample string is for is testing. this is it .”.

/*
 * Erase all Occurrences of all given substrings from main string using C++11 stuff
 */
void eraseSubStrings(std::string & mainStr, const std::vector<std::string> & strList)
{
	// Iterate over the given list of substrings. For each substring call eraseAllSubStr() to
	// remove its all occurrences from main string.
	std::for_each(strList.begin(), strList.end(), std::bind(eraseAllSubStr, std::ref(mainStr), std::placeholders::_1));
}

Iterate over the vector of given sub string and for each one erase all occurrences of it from string.

Erase all Occurrences of all given sub strings from main string using Pre C++11 stuff

/*
 * Erase all Occurrences of all given substrings from main string using Pre C++11 stuff
 */
void eraseSubStringsPre(std::string & mainStr, const std::vector<std::string> & strList)
{
	// Iterate over the given list of substrings. For each substring call eraseAllSubStr() to
	// remove its all occurrences from main string.
	for (std::vector<std::string>::const_iterator it = strList.begin(); it != strList.end(); it++)
	{
		eraseAllSubStr(mainStr, *it);
	}

}

Complete example is as follows,

#include <iostream>
#include <string>
#include <algorithm>
#include <functional>

/*
 * Erase First Occurrence of given  substring from main string.
 */
void eraseSubStr(std::string & mainStr, const std::string & toErase)
{
	// Search for the substring in string
	size_t pos = mainStr.find(toErase);

	if (pos != std::string::npos)
	{
		// If found then erase it from string
		mainStr.erase(pos, toErase.length());
	}
}

/*
 * Erase all Occurrences of given substring from main string.
 */
void eraseAllSubStr(std::string & mainStr, const std::string & toErase)
{
	size_t pos = std::string::npos;

	// Search for the substring in string in a loop untill nothing is found
	while ((pos  = mainStr.find(toErase) )!= std::string::npos)
	{
		// If found then erase it from string
		mainStr.erase(pos, toErase.length());
	}
}

/*
 * Erase all Occurrences of all given substrings from main string using C++11 stuff
 */
void eraseSubStrings(std::string & mainStr, const std::vector<std::string> & strList)
{
	// Iterate over the given list of substrings. For each substring call eraseAllSubStr() to
	// remove its all occurrences from main string.
	std::for_each(strList.begin(), strList.end(), std::bind(eraseAllSubStr, std::ref(mainStr), std::placeholders::_1));
}

/*
 * Erase all Occurrences of all given substrings from main string using Pre C++11 stuff
 */
void eraseSubStringsPre(std::string & mainStr, const std::vector<std::string> & strList)
{
	// Iterate over the given list of substrings. For each substring call eraseAllSubStr() to
	// remove its all occurrences from main string.
	for (std::vector<std::string>::const_iterator it = strList.begin(); it != strList.end(); it++)
	{
		eraseAllSubStr(mainStr, *it);
	}

}

int main()
{
	std::string str = "Hi this is a sample string is for is testing is.";

	eraseAllSubStr(str, "this");

	std::cout << str << std::endl;

	eraseSubStringsPre(str, { "for", "is", "testing" });

	std::cout << str << std::endl;
	return 0;
}

Output

Hi  is a sample string is for is testing is.
Hi   a sample string

 

 

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