亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在標準字符串中搜索/查找和替換?

如何在標準字符串中搜索/查找和替換?

C++
守候你守候我 2019-12-12 14:09:58
有沒有辦法用另一個字符串替換所有出現的子字符串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();

  }

}



查看完整回答
反對 回復 2019-12-13
?
波斯汪

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 的官方文檔。


查看完整回答
反對 回復 2019-12-13
  • 3 回答
  • 0 關注
  • 350 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號