snippetcppCritical
How to replace all occurrences of a character in string?
Viewed 0 times
replacehowoccurrencescharacterstringall
Problem
What is the effective way to replace all occurrences of a character with another character in
std::string?Solution
std::string doesn't contain such function but you could use stand-alone replace function from algorithm header.#include
#include
void some_func() {
std::string s = "example string";
std::replace( s.begin(), s.end(), 'x', 'y'); // replace all 'x' to 'y'
}Code Snippets
#include <algorithm>
#include <string>
void some_func() {
std::string s = "example string";
std::replace( s.begin(), s.end(), 'x', 'y'); // replace all 'x' to 'y'
}Context
Stack Overflow Q#2896600, score: 999
Revisions (0)
No revisions yet.