c ++替换<<之间的一组字符串>>分隔符

时间:2015-03-09 05:39:50

标签: c++ regex boost

我想在<<之间替换一组字符串>>分隔符。

例如说

int age= 25;
string name= "MYNAME";

string test = My age is << your age >> and my name is << your name >>. 

输出应为

My age is 25 and my name is MYNAME. 

执行此操作的最佳方法是c ++?

2 个答案:

答案 0 :(得分:1)

试试这个

#include <iostream>       
#include <string>        

int main ()
{
  std::string str ("My age is << your age >> and my name is << your name >>.");
  std::string str2 ("<< your age >>");
  std::string str3 ("<< your name >>");
  str.replace(str.find(str2),str2.length()," 22 ");
  str.replace(str.find(str3),str3.length()," Nooh ");
  std::cout << str << '\n';
  return 0;
}

答案 1 :(得分:-1)

我不确定我是否理解这个问题,但如果我认为它是,请尝试

string test = "My age is " + age + " and my name is " + name + ".";

如果您倾向于使用&lt;&lt;和&gt;&gt;,你可以做

cout << "My age is " << age << " and my name is " << name << "." << endl;