有沒有辦法用另一個字符串替換所有出現的子字符串std::string?例如:void SomeFunction(std::string& str){ str = str.replace("hello", "world"); //< I'm looking for something nice like this}
3 回答

嚕嚕噠
TA貢獻1784條經驗 獲得超7個贊
為什么不實施自己的替換?
void myReplace(std::string& str,
const std::string& oldStr,
const std::string& newStr)
{
std::string::size_type pos = 0u;
while((pos = str.find(oldStr, pos)) != std::string::npos){
str.replace(pos, oldStr.length(), newStr);
pos += newStr.length();
}
}

波斯汪
TA貢獻1811條經驗 獲得超4個贊
#include <boost/algorithm/string.hpp> // include Boost, a C++ library
...
std::string target("Would you like a foo of chocolate. Two foos of chocolate?");
boost::replace_all(target, "foo", "bar");
這是關于replace_all 的官方文檔。
- 3 回答
- 0 關注
- 350 瀏覽
添加回答
舉報
0/150
提交
取消