string.replace(startIndex, length, newString);
#include#include int main() { std::string str = "Quick brown fox"; str.replace(6, 5, "red"); std::cout << "Modified string: " << str << '\n'; return 0; }
Modified string: Quick red fox
#include#include int main() { std::string str = "To be or not to be, that is the question."; std::string target = "be"; std::string replacement = "not be"; size_t pos = 0; while ((pos = str.find(target, pos)) != std::string::npos) { str.replace(pos, target.length(), replacement); pos += replacement.length(); } std::cout << "Modified string: " << str << '\n'; return 0; }
Modified string: To not be or not to not be, that is the question.Package library: Standard C++ library.