What is the best way to replace characters in a string?
Specifically:
"This,Is A|Test" ----> "This_Is_A_Test"
I want to replace all commas, spaces, and "|" with underscores.
(I have access to Boost.)
You could use the standard replace_if
algorithm, except the predicate is rather complicated (to be expressed inline with current C++ standard and no lambda).
You could write your own, or use is_any_of
from boost's string algorithms, so:
#include <algorithm>
#include <string>
#include <boost/algorithm/string/classification.hpp>
#include <iostream>
int main()
{
std::string s("This,Is A|Test");
std::replace_if(s.begin(), s.end(), boost::is_any_of(", |"), '_');
std::cout << s << '\n';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With