6

Find and Replace all occurrences of a sub string in C++

 3 years ago
source link: https://thispointer.com/find-and-replace-all-occurrences-of-a-sub-string-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this article we will discuss how to replace all occurrences of a sub string with new string in C++.

For example, we have a string i.e.

“Boost Library is simple C++ Library”

And we want replace all occurrences of ‘Lib’ with XXX. Let’s see the different methods to do it,

Find & Replace all sub strings – using STL

#include <iostream>
#include <string>
void findAndReplaceAll(std::string & data, std::string toSearch, std::string replaceStr)
// Get the first occurrence
size_t pos = data.find(toSearch);
// Repeat till end is reached
while( pos != std::string::npos)
// Replace this occurrence of Sub String
data.replace(pos, toSearch.size(), replaceStr);
// Get the next occurrence from the current position
pos =data.find(toSearch, pos + replaceStr.size());
int main()
std::string data = "Boost Library is simple C++ Library";
std::cout<<data<<std::endl;
findAndReplaceAll(data, "Lib", "XXX");
std::cout<<data<<std::endl;
return 0;
#include <iostream>
#include <string>

void findAndReplaceAll(std::string & data, std::string toSearch, std::string replaceStr)
{
	// Get the first occurrence
	size_t pos = data.find(toSearch);

	// Repeat till end is reached
	while( pos != std::string::npos)
	{
		// Replace this occurrence of Sub String
		data.replace(pos, toSearch.size(), replaceStr);
		// Get the next occurrence from the current position
		pos =data.find(toSearch, pos + replaceStr.size());
	}
}

int main()
{
	std::string data = "Boost Library is simple C++ Library";

	std::cout<<data<<std::endl;

	findAndReplaceAll(data, "Lib", "XXX");

	std::cout<<data<<std::endl;


	return 0;
}

Output:
Boost Library is simple C++ Library
Boost XXXrary is simple C++ XXXrary
Boost Library is simple C++ Library
Boost XXXrary is simple C++ XXXrary

Find & Replace all sub strings – using Boost::replace_all

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
int main()
std::string data = "Boost Library is simple C++ Library";
std::cout<<data<<std::endl;
// Replace all occurrences of 'LIB' with 'XXX'
// Case Sensitive Version
boost::replace_all(data, "Lib", "XXX");
std::cout<<data<<std::endl;
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>



int main()
{
	std::string data = "Boost Library is simple C++ Library";

	std::cout<<data<<std::endl;

	// Replace all occurrences of 'LIB' with 'XXX'
	// Case Sensitive Version
	boost::replace_all(data, "Lib", "XXX");

	std::cout<<data<<std::endl;


}

Output:
Boost Library is simple C++ Library
Boost XXXrary is simple C++ XXXrary
Boost Library is simple C++ Library
Boost XXXrary is simple C++ XXXrary

Find & Replace all Case Insensitive Sub Strings using Boost::ireplace_all

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
int main()
std::string data = "Boost Library is simple C++ Library";
std::cout<<data<<std::endl;
// Replace all occurrences of 'LIB' with 'XXX'
// Case Insensitive Version
boost::ireplace_all(data, "LIB", "XXX");
std::cout<<data<<std::endl;
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>



int main()
{
	std::string data = "Boost Library is simple C++ Library";

	std::cout<<data<<std::endl;

	// Replace all occurrences of 'LIB' with 'XXX'
	// Case Insensitive Version
	boost::ireplace_all(data, "LIB", "XXX");

	std::cout<<data<<std::endl;


}

Output:
Boost Library is simple C++ Library
Boost XXXrary is simple C++ XXXrary
Boost Library is simple C++ Library
Boost XXXrary is simple C++ XXXrary

2 Comments Already

  1. e177ba8fb97ec4ed11e7cbb0aeb9622a?s=52&d=mm&r=g

    dang d. khanh - January 16th, 2021 at 10:00 pm none Comment author #41857 on Find and Replace all occurrences of a sub string in C++ by thispointer.com

    thanks for sharing tips. first post i read!
    i hope can read all your tip in the next week ^^.

  2. e177ba8fb97ec4ed11e7cbb0aeb9622a?s=52&d=mm&r=g

    dang d. khanh - January 16th, 2021 at 10:11 pm none Comment author #41858 on Find and Replace all occurrences of a sub string in C++ by thispointer.com

Leave a Reply Cancel reply

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

Name *

Email *

Website

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK