C++ Replace Characters in String Guide
To replace characters in a string, you can use the std::replace function or implement your own replacement function. Here is an example using the std::replace function:
#include <iostream>
#include <algorithm>
#include <string>
int main() {
std::string str = "Hello, World!";
std::replace(str.begin(), str.end(), 'l', 'x');
std::cout << str << std::endl; // 输出Hexxo, Worxd!
return 0;
}
This code replaces all instances of the character ‘l’ with ‘x’ in a string. If multiple characters need to be replaced, the std::replace function can be called multiple times. Alternatively, for more complex replacements, consider implementing a custom replacement function.