String replace() in C++
The replace() function in C++ is used to replace a part of a string with another string. This function takes three parameters:
- The starting index of the part of the string to be replaced.
- The length of the part to be replaced.
- The replacement string.
The replace() function returns a new string with the specified part replaced by the replacement string.
Syntax
The syntax of the replace() function is as follows:
string replace(size_t index, size_t length, const string& replacement);
Here, the index is the starting index of the part of the string to be replaced, length is the length of the part to be replaced, and replacement is the replacement string.
Example usage of the replace() function:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "The quick brown fox jumps over the lazy dog";
string newStr = str.replace(16, 5, "grey cat");
cout<< "Original string: " << str <<endl;
cout<< "New string: " <<newStr<<endl;
return 0;
}
Output:
Original string: The quick brown fox jumps over the lazy dog
New string: The quick brown grey cat jumps over the lazy dog
Explanation:
In the above example, we first define a string str and then call the replace() function on it to replace the substring "fox" with the string "grey cat". After that, we store the new string in the variable newStr and print both the original and new strings to the console.
Here is another example that shows how to use the replace() function to replace multiple occurrences of a substring in a string:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "The quick brown fox jumps over the lazy dog";
string searchStr = "the";
string replaceStr = "THE";
size_t pos = 0;
while ((pos = str.find(searchStr, pos)) != string::npos)
{
str.replace(pos, searchStr.length(), replaceStr);
pos += replaceStr.length();
}
cout<< "Original string: " << "The quick brown fox jumps over the lazy dog" <<endl;
cout<< "New string: " << str <<endl;
return 0;
}
Output:
Original string: The quick brown fox jumps over the lazy dog
New string: The quick brown fox jumps over THE lazy dog
Explanation:
In the above example, we first define a string str and two other strings searchStr and replaceStr. We also define variable pos to keep track of the position of the substring we are looking for. After that, we use a while loop that iterates over the string and uses the find() function to find the next occurrence of the substring we are looking for. If the substring is found, we call the replace() function to replace it with the replacement string. After that, we update the value of pos to continue searching for the next occurrence of the substring. Finally, we print both the original and new strings to the console.
Here are some additional examples to illustrate the use of the replace() function:
Example 1: Replacing a single character in a string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "hello world";
str.replace(1, 1, "E");
cout<< str <<endl;
return 0;
}
Output:
hEllo world
Explanation:
In this example, we replace the second character in the string "hello world" (index 1) with the character "E".
Example 2: Replacing a substring with an empty string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "remove these spaces please";
str.replace(7, 5, "");
cout<< str <<endl;
return 0;
}
Output:
remove spaces please
Explanation:
In this example, we remove the substring "these" from the string "remove these spaces please" by replacing it with an empty string.
Example 3: Replacing a substring with a longer string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "replace these two words";
str.replace(8, 4, "this phrase");
cout<< str <<endl;
return 0;
}
Output:
replace this phrase two words
In this example, we remove the substring "these" from the string "replace these two words" by replacing it with a "this phrase".
Conclusion
In conclusion, the replace() function in C++ is a useful tool for manipulating strings. It allows us to replace parts of a string with other strings and is particularly useful when we need to replace multiple occurrences of a substring. By understanding how to use this function, we can write more efficient and effective code for manipulating strings in our C++ programs.