replace character in string c++ code example
Example 1: c++ replace character in string
#include <algorithm>
#include <string>
void some_func() {
std::string s = "example string";
std::replace( s.begin(), s.end(), 'x', 'y');
}
Example 2: c++ replace character in string
#include <string>
#include <regex>
using namespace std;
string test = "abc def abc def";
test = regex_replace(test, regex("abc"), "Ziv");
Example 3: replace a char in string c++ at a specific index
string a = "lol";
a[2] = 'j';
cout << a;