Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在字符串中查找字符串并替换它?_C++_String_Boost - Fatal编程技术网

C++ 如何在字符串中查找字符串并替换它?

C++ 如何在字符串中查找字符串并替换它?,c++,string,boost,C++,String,Boost,我有一个std::string a我需要在其中找到字符串B和bla-bla-bla之类的内容,并用其他字符串C替换它,比如abcdefg,如果找不到B,只需将C放在a的开头 怎么做这样的事 A.replace(str.find(B), B.length(), C); 您可能需要添加错误检查;-) 当你有一些url,你想让它使用类似“rtmp://”的东西而不是“http://”时,它是完全使用的。当然,你知道是不是“http://”)你看过string::replace和string::fin

我有一个
std::string a
我需要在其中找到字符串
B
bla-bla-bla
之类的内容,并用其他字符串
C
替换它,比如
abcdefg
,如果找不到
B
,只需将
C
放在
a
的开头

怎么做这样的事

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

您可能需要添加错误检查;-)

当你有一些url,你想让它使用类似“rtmp://”的东西而不是“http://”时,它是完全使用的。当然,你知道是不是“http://”)你看过string::replace和string::find了吗?+1/听起来很有逻辑,但最好把b和c作为
const std::string&
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);
  }
}