开发者

How to find a string in string and replace it?

I have a std::string A I need to find开发者_JS百科 in it string B with content like bla-bla-bla and replace it with other string C like abcdefg and if B was not found just put C at the beginning of A.

How to do such thing?


void replace_or_merge(std::string &a, const std::string &b, const std::string &c)
{
  const std::string::size_type pos_b_in_a = a.find(b);
  if(pos_b_in_a == std::string::npos) {
    a.insert(0, c);
  }
  else {
    a.replace(pos_b_in_a, b.length(), c);
  }
}


A.replace(str.find(B), B.length(), C);

You might want to add error checking ;-)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜