Find and Replace All Occurrences in a C++ String

DevOps Engineer | Kubernetes | Python | Terraform | AWS | GCP
Search for a command to run...

DevOps Engineer | Kubernetes | Python | Terraform | AWS | GCP
No comments yet. Be the first to comment.
Tired of shuffling USB cables every time someone in your household or small office needs to print? Wish you could print wirelessly from any device on your network? Well, you're in luck! With the magic of a Raspberry Pi and a few simple commands, you ...

It is quite common to lose the integrity of configmaps/secrets for the following reasons: You have a large team with more than 5 people You do not use any Config/Secret Management Tool Lack of team collaboration Anyway, that's not the point. All...

It's quite common to see your important service account being modified by someone. Don't worry, my friend. Here is how you can track who did what. Login to GCP and navigate to Logging Set a proper timeline from the date-time picker (last X hour or ...

You can speed up your smart tv experience by disabling some annoying, built in apps. First, enable ADB on your Android TV unit. To Activate Developer Options: Navigate to Settings > About. Tap Build number seven times to enable developer mode. Once d...

If you need to automate the process of clicking a button on a webpage using Selenium, this guide will walk you through it effortlessly. Prerequisites You will need the following items. Python3 with selenium module Chrome Browser and Chrome Driver [...

I am familiar with QString and always use its overloaded functions like replace(), remove() etc. It has automatic regex detection and very suitable for OOP. But while using pure C++ (I mean no external API or library), I faced some problems. I needed to find and replace ALL OCCURENCES of find text in a string. std::string has replace function. But it requires string iterator, text length etc. Again, it replaces only one string part at a time. So if you need a replace_all function, You can do the following. I got this from stackoverflow.com (unfortunately I forgot the link).
void find_and_replace(string& source, string const& find, string const& replace)
{
for(string::size_type i = 0; (i = source.find(find, i)) != string::npos;)
{
source.replace(i, find.length(), replace);
i += replace.length();
}
}
The program is very simple. It finds string position, replaces by given text length and does it until the find position is at npos.
This is a sample code.
#include <iostream>
using namespace std;
void find_and_replace(string& source, string const& find, string const& replace)
{
for(string::size_type i = 0; (i = source.find(find, i)) != string::npos;)
{
source.replace(i, find.length(), replace);
i += replace.length();
}
}
int main()
{
string text;
// simple replace
text = "i have a blue house and a blue car";
cout << "string: " << text << endl;
find_and_replace(text, "blue", "red");
cout << "replace: " << text << endl;
cout << endl;
// simple replace 2
text = "i love apple";
cout << "string: " << text << endl;
find_and_replace(text, "apple", "banana");
cout << "replace: " << text << endl;
cout << endl;
// simple replace 3
text = "some-words-separated-by-hyphen";
cout << "string: " << text << endl;
find_and_replace(text, "-", "_");
find_and_replace(text, "hyphen", "underscore");
cout << "replace: " << text << endl;
cout << endl;
// replace with empty string
text = "this string has an is missing";
cout << "string: " << text << endl;
find_and_replace(text, "is", "");
cout << "replace: " << text << endl;
cout << endl;
// replace with space
text = "hello;world;";
cout << "string: " << text << endl;
find_and_replace(text, ";", " ");
cout << "replace: " << text << endl;
return 0;
}
string: i have a blue house and a blue car
replace: i have a red house and a red car
string: i love apple
replace: i love banana
string: some-words-separated-by-hyphen
replace: some_words_separated_by_underscore
string: this string has an is missing
replace: th string has an msing
string: hello;world;
replace: hello world