How to replace all occurrences of a character in string?
What is the effective way to replace all occurrences of a character with another character in `std::string`?
Jimwel A.
answered 12/24/19
Software Development Engineer Experienced in C++
There are multiple ways to do it.
One way is to use the replace function from the algorithm header:
#include <string>
#include <algorithm>
std::string foo = "Hello World";
std::replace( foo.begin(), foo.end(), 'l', 'w');
Still looking for help? Get the right answer, fast.
OR
Find an Online Tutor Now
Choose an expert and meet online.
No packages or subscriptions, pay only for the time you need.