C++ で文字列内の部分文字列を別の部分文字列に置き換えるにはどうすればよいですか。どのような関数を使用できますか。
eg: string test = "abc def abc def";
test.replace("abc", "hij").replace("def", "klm"); //replace occurrence of abc and def with other substring
ベストアンサー1
でC++11 の、使用することができますstd::regex_replace
:
#include <string>
#include <regex>
std::string test = "abc def abc def";
test = std::regex_replace(test, std::regex("def"), "klm"); // replace 'def' -> 'klm'
// test = "abc klm abc klm"