【问题标题】:c++ replace a set of strings between << >> delimetersc ++替换<<>>分隔符之间的一组字符串
【发布时间】:2015-03-09 05:39:50
【问题描述】:

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

比如说

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++ 最好的方法是什么?

【问题讨论】:

标签: c++ regex boost


【解决方案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;
}

【讨论】:

    【解决方案2】:

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

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

    如果你一心想使用>,你可以这样做

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2013-09-20
      • 1970-01-01
      相关资源
      最近更新 更多