Archive

Some days ago, we talked about how to replace substrings inside a string in C++. We finally got a method to just copy and paste into our projects, but when we want to replace multiple substrings we will get some ugly code, and some times it won’t fit.

We will use, one common container in C++ called map, it’s just a collection of associations between two values, we can see it as an array of key and value elements. So we will associate some substrings with another substring (we will associate fromStrs with toStrs. We also will make a replace() function accepting two initial arguments (the big string, and the map), we will look for each one of the keys and replace them like this:

In this case, we can add as much elements as we want to the map, and all of them will be searched in the big string. This function is good when we don’t know the fromStr and toStr in compilation time (we can generate them in runtime), we want to fill the map little by little and then do all replacements at once.

But we can have a little problem, when some toStr are contained inside some fromStr and vice versa, this function won’t work as expected. We will have to iterate the map for each one of the substitutions, instead of doing it globally and make single replacements (like we did with the old replace):

Original string: If a black bug bleeds black blood, what color blood does a blue bug bleed?
Resulting string: If a blue bug bleeds blue blood, what color blood does a black bug bleed?

A bit tongue twisting but I think you’ve got the idea.

One more interesting thing is the map creation. Lot’s of times we will have a clear idea of what elements go in the map. So we don’t want to spend time adding elements one by one. As I told you some days ago, you can pass a variable number of arguments to a C function, so, why not do it here? Let’s pass the strings as char* and add them to the map:

Don’t forget the last NULL, because it can cause a disaster in runtime (not always, you may have luck, but so often), due to strMap condition to stop reading arguments, it stops when it sees a NULL there, if you don’t put it, maybe it is there yet, or maybe not.

One of the most useful tools when programming is searching and replacing text from a string. In other words, searching for substrings inside a bigger string and replacing them with other substrings. For example, we can use templates to generate a message or our desired output, we just have to replace some keywords located inside our template (our big string) with our generated data.

We will do it calling standard string methods to find this substring and then replacing it with another one:

12345678910111213141516171819

#include <iostream>#include <string>

usingnamespace std;

int main(){
string original ="Going to sleep. Everyone knows past 12 we must go to sleep.";

In this case, we use method find to search for the position where the substring “sleep” starts. If the string is not found, it returns string::npos (a constant), but if it is, we will replace it with the word “code”, so we will have to say the start position inside original, and the size of the substring we want to replace.

Now, what we want is to replace all occurrences of “sleep” inside the string, so we just have to loop find() and replace() while we are still finding the substring, this way:

12345678910111213141516171819202122232425262728

#include <iostream>#include <string>

usingnamespace std;

int main(){
string original ="Going to sleep. Everyone knows past 12 we must go to sleep.";

string::size_type pos =0;

// No we are using these variables with the substring to be replaced and the replacement.
string fromStr ="sleep";
string toStr ="code";

If we run this code, the two occurrences of “sleep” will be replaced by “code”. But we find a interesting comment in the code: “to avoid infinite loops”, what we do is adding the size of the string “code” to the pos variable. This would be a special case when the substring to be replaced is contained inside the replacement string (or they both are the same), in other words, if we search “to” and we want to replace it by “ton” and we comment this line. It will do the replacement with no ending, and if we cout the string inside the loop we will se “tonnnnnn … with a growing number of n”.

We have now a good way to do search and replace, so let’s create a function to simplify the usage, but with an added value: we can give an offset value (so we can choose where to start searching in the original string, just giving pos a non-zero value), and a counter, so we can replace fromStr with toStr a number of times, so if our keyword appears ten times, we can choose to replace it five times.

Original string: Going to sleep. Everyone knows past 12 we must go to sleep. So if I sleep tonight, I don’t have to sleep tomorrow morning
Resulting string: Going to sleep. Everyone knows past 12 we must go to code. So if I code tonight, I don’t have to sleep tomorrow morning.

So, we start searching substring “sleep” from character number 20, and then replace this substring twice. The replace function has initial values, so if we omit offset we will start from the beginning of the string, and if we omit the number of times, this will be zero, and it means all occurrences will be replaced.

To make it better, we can make use of Glib::ustring instead of string. Glib is a cross-platform utility library that implements its own version of string with UTF8 support. So if we use special characters like tildes or symbols we will have encoding problems. We can test it this way:

1234567

#include <string>

int main(){cout<< string("piñata").length()<< endl;return0;}

Let’s see the output. We know “piñata” has six letters, but sometimes we will get seven because we are using a multibyte encoding (like UTF8 wich uses two bytes to encode “ñ”). There it goes Glib::ustring, this library (free, of course), behaves exactly like string (but with UTF-8 support. Let’s see: