Replace all occurrences of a substring in a string in C++
This post will discuss how to replace all occurrences of a substring in a string in C++.
1. Using string::find
There is no built-in function to replace all occurrences of a substring in a string in C++. To find all instances of a substring in a string, we can call the string::find function, and replace each occurrence with the string::erase and string::insert functions. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> #include <string> int main() { std::string s = "C++17"; std::string x = "17"; std::string y = "20"; size_t pos = 0; while (pos += y.length()) { pos = s.find(x, pos); if (pos == std::string::npos) { break; } s.erase(pos, x.length()); s.insert(pos, y); } std::cout << s << std::endl; // C++20 return 0; } |
The call to the string::erase and string::insert function can be replaced with a single call to the string::replace function, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <string> int main() { std::string s = "C++17"; std::string x = "17"; std::string y = "20"; size_t pos = 0; while (pos += y.length()) { pos = s.find(x, pos); if (pos == std::string::npos) { break; } s.replace(pos, x.length(), y); } std::cout << s << std::endl; // C++20 return 0; } |
2. Using Boost
If you’re using the boost string algorithm library in your project, you can use the boost::replace_all algorithm from the <boost/algorithm/string/replace.hpp> header. The following C++ program demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <boost/algorithm/string/replace.hpp> int main() { std::string s = "C++17"; std::string x = "17"; std::string y = "20"; boost::replace_all(s, x, y); std::cout << s << std::endl; // C++20 return 0; } |
To avoid any modification on the original string, consider using the boost::replace_all algorithm.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <boost/algorithm/string/replace.hpp> int main() { std::string s = "C++17"; std::string x = "17"; std::string y = "20"; s = boost::replace_all_copy(s, x, y); std::cout << s << std::endl; // C++20 return 0; } |
3. Using std::regex_replace
Starting with C++11, we can use the std::regex_replace function for this. Its usage is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <regex> int main() { std::string s = "C++17"; std::string x = "17"; std::string y = "20"; s = std::regex_replace(s, std::regex(x), y); std::cout << s << std::endl; // C++20 return 0; } |
That’s all about replacing all occurrences of a substring in a string in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)