在C++中是否有等效的str_replace?

wai*_*933 7 c++ replace

在PHP中,有一个str_replace基本上可以查找和替换的函数.在C++中是否有这个功能的等价物?

Geo*_*che 9

不完全是这样,但是看一下Boost字符串算法库 - 在这种情况下是替换函数:

std::string str("aabbaadd");    
boost::algorithm::replace_all(str, "aa", "xx");
Run Code Online (Sandbox Code Playgroud)

str现在包含"xxbbxxdd".

  • @Kugel:你的目标是什么?字符串算法是抽象的,并与任何字符串实现[要求](http://www.boost.org/doc/libs/1_43_0/doc/html/string_algo/design.html#string_algo.string)工作.所以请使用`wstring`,将Boost.Locale与ICU或其他最适合的东西一起使用. (3认同)

Ant*_*ony 7

std::string::replace会做替换.您可以将其与std::string::find*方法结合以获得类似的功能.它并不像PHP方式那么容易.我认为Boost有你想要的东西; 在正则表达式中.