-2

I want to replace a set of strings between << >> delimiters.

For example say

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

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

Output should be

My age is 25 and my name is MYNAME. 

What is the best method to do this is c++ ?

  • I don't quite understand your question. Why do you want to use these "delimiters" exactly? – Jérémi Panneton Mar 09 '15 at 05:50
  • 1
    Show your effort what you tried, and people here would surely help you. – Mohit Jain Mar 09 '15 at 06:00
  • http://stackoverflow.com/questions/28601603/how-to-parse-using-boost-if-it-is-not-json-but-similar-to-it/28602037#28602037, http://stackoverflow.com/questions/17112494/how-to-expand-environment-variables-in-ini-files-using-boost/17126962#17126962, http://stackoverflow.com/questions/9404558/compiling-a-simple-parser-with-boost-spirit/9405546#9405546 – sehe Mar 09 '15 at 08:55

2 Answers2

1

try this

#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;
}
Nooh
  • 1,548
  • 13
  • 21
-1

Im not sure I understand the question, but if its what I think it is, try

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

If you are bent on using << and >>, you can do

cout << "My age is " << age << " and my name is " << name << "." << endl;
Meeoh
  • 463
  • 4
  • 11