开发者

Is there an equivalent of str_replace in C++?

开发者 https://www.devze.com 2023-01-03 22:53 出处:网络
In PHP, there is a str_replace function 开发者_如何转开发that basically does a find and replace. Is there an equivalent of this function in C++?Not exactly, but take a look at the Boost String Algorit

In PHP, there is a str_replace function 开发者_如何转开发that basically does a find and replace. Is there an equivalent of this function in C++?


Not exactly, but take a look at the Boost String Algorithms Library - in this case the replace functions:

std::string str("aabbaadd");    
boost::algorithm::replace_all(str, "aa", "xx");

str now contains "xxbbxxdd".


std::string::replace will do replacement. You can couple it with std::string::find* methods to get similar functionality. It's not as easy as the PHP way. I think Boost has what you're looking for though; in regular expressions.


You can also use std::regex_replace

0

精彩评论

暂无评论...
验证码 换一张
取 消