The string object is a data structure, and its main component is a list. Each cell of this list has a character. The total sequence of characters forms the literal string. Each character position can be accessed by an index or by an iterator. Index counting begins from zero. An iterator is an elaborated pointer.
The C+ string class has different variants of the replace() member function. Such a set of functions are called overloaded functions. This article explains how to use different overloaded string replace() member functions.
The following target string will be used for all the examples:
The element (character) of a string object can either be accessed by a reference or by an iterator. C++20 actually has 14 overloaded replace() member functions for the string class. With seven of these functions, the elements are accessed by reference, while with the other seven, the elements are accessed by an iterator.
The seven-member functions, for which the elements are accessed by reference, will be explained in this article. Only one for the remaining seven, for iterators, will be explained. With the knowledge of that one, for which the iterator accesses elements, the reader can deduce the explanation for the remaining six.
basic_string& replace(size_type pos1, size_type n1, const T& t)
This member function goes to the index at pos1, then measures a sub-string of length n1 (beginning from pos1) and replaces it with another independent sub-string of array-of-chars. The sub-string is to be replaced, and the sub-string does not have to be of the same length. The following program illustrates this:
#include <iostream>
using namespace std;
int main()
{
string target = string("Our planet has 24 hours a day.");
char replacement[] = "solar home";
string result = target.replace(4, 6, replacement);
cout << result << endl;
return 0;
}
The output is:
The returned string is a string object. To use the replace() member function of the string class, the string library has to be included in the program (with a directive).
Note: all string code for this article is in the C++ main function.
basic_string& replace(size_type pos, size_type n1, const charT* s)
This member function is almost the same as the above. It goes to the index at pos, measures a sub-string of length n1 (beginning from pos), and replaces it with another independent sub-string of a constant-pointer-to-chars. The replaced sub-string and replacing sub-string do not have to be of the same length. The following program illustrates this:
#include <iostream>
using namespace std;
int main()
{
string target = string("Our planet has 24 hours a day.");
const char* replacement = "solar home";
string result = target.replace(4, 6, replacement);
cout << result << endl;
return 0;
}
The output is:
basic_string& replace(size_type pos1, size_type n1, const basic_string& str)
This member function is similar to the above. However, the replacing string is a string object, instantiated from the string class. The following program illustrates this:
#include <iostream>
using namespace std;
int main()
{
string target = string("Our planet has 24 hours a day.");
string replacement = string("solar home");
string result = target.replace(4, 6, replacement);
cout << result << endl;
return 0;
}
The output is:
basic_string& replace(size_type pos, size_type n1, size_type n2, charT c)
This member function is similar to the above. However, the replacing string is the same character, repeated n2 times. Note that there are four parameters for this function. The following program illustrates its use:
#include <iostream>
using namespace std;
int main()
{
string target = string("Our planet has 24 hours a day.");
string result = target.replace(4, 6, 3, 'e');
cout << result << endl;
return 0;
}
The output is:
"planet" was replaced by three e's (for earth).
basic_string& replace(size_type pos, size_type n1, const charT* s, size_type n2)
This member function is similar to the above. However, the replacement string is a constant pointer to chars, whose first n2 characters are used for replacement. The following program illustrates this:
#include <iostream>
using namespace std;
int main()
{
string target = string("Our planet has 24 hours a day.");
const char* replacement = "earth world";
string result = target.replace(4, 6, replacement, 5);
cout << result << endl;
return 0;
}
The output is:
basic_string& replace(size_type pos1, size_type n1, const T& t, size_type pos2, size_type n2 = npos)
This member function is similar to the above. However, the replacing string is an array-of-chars whose n2 characters are used for replacement, beginning from a position of the replacing string, pos2, which is not necessarily zero. If the fourth argument of this function is absent, then the number of characters for replacement will be from pos2 to the end of the array-of-chars; otherwise, it will be n2 characters from pos2. The following program illustrates this:
#include <iostream>
using namespace std;
int main()
{
string target = string("Our planet has 24 hours a day.");
char replacement[] = "the earth world";
string result = target.replace(4, 6, replacement, 4, 5);
cout << result << endl;
return 0;
}
The output is:
basic_string& replace(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2 = npos)
This member function is similar to the above. However, the replacing string is a string object, instantiated from the string class. The following code illustrates this:
#include <iostream>
using namespace std;
int main()
{
string target = string("Our planet has 24 hours a day.");
string replacement = string("the earth world");
string result = target.replace(4, 6, replacement, 4, 5);
cout << result << endl;
return 0;
}
The output is:
basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str)
This member function goes to the element (character) of the target string, pointed to, by the iterator, i1; it then considers characters up to the one pointed to, by the iterator, i2. This range is to be replaced by all the characters of a string object, instantiated from a class. The following program illustrates this:
#include <iostream>
using namespace std;
int main()
{
string target = string("Our planet has 24 hours a day.");
string replacement = string("solar world");
string::iterator it1 = target.begin();
it1 = it1 + 4;
string::iterator it2 = target.begin();
it2 = it2 + 10;
string result = target.replace(it1, it2, replacement);
cout << result << endl;
return 0;
}
The output is:
The last element of the range, pointed to by the second iterator, did not take part in the replacement; it was just the upper delimiter.
Two iterators, it1 and it2, have been used. Each has been obtained from the begin() member function of the string class. The begin() member function returns an iterator, which points to the first element of the string object. To make it1 point to 'p' of "planet" at index 4 of the target string, it1 had to be increased by 4. To make it2 point to '’ just after “planet” at index 10 of the target string, it2 had to be increased by 10.
Conclusion
It is a sub-string of the list of a string object, instantiated from a string class, that is considered for replacement. A substring of a string declared as array-of-chars or constant pointer to chars is not considered for replacement. The replacing string is another independent string. The whole string literal of the replacing string can do the replacement, or a portion of it can still do the replacement. The C++20 string class has 14 member overloaded functions for replacement, 8 of which have been explained above.