3 Replies - 632 Views - Last Post: 11 April 2012 - 03:39 PM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Replacing strings with numbers

Posted 11 April 2012 - 02:45 PM

Hi everybody,
I have a string that has multiple standardized substrings (in CAPS) in it, and I'd like to replace those substrings with numbers. For example, I have:

int one = 5;
double two = 77.14;
double three = 999.67;
int four = 2;
string template = "some stuff ONE more TWO even more stuff THREE stuff FOUR";
string result = "";



and I want to turn it into this:
int one = 5;
double two = 77.14;
double three = 999.67;
int four = 2;
string template = "some stuff 5 more 77.14 even more stuff 999.67 stuff 2";
string result = "";



I know you can use str_replace to accomplish this, but after reading up on it I'm more confused than how I started. I also know I can convert the numbers to strings using a string stream, but that seems like it might be overkill, and I have a feeling I'm missing a simpler solution.

Would anyone mind giving me an example with str_replace of how this could be done, with an explanation, please? All I need is one clearly explained example and then I'll be off and running;

If anyone could give me some help here, I'd really appreciate it.

Thanks,
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: Replacing strings with numbers

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 353
  • View blog
  • Posts: 774
  • Joined: 27-June 09

Re: Replacing strings with numbers

Posted 11 April 2012 - 02:59 PM

Try this

string template = "some stuff ONE more TWO even more stuff THREE stuff FOUR";
template = template.replace("ONE", "5");
template = template.replace("TWO", "77.14");
template = template.replace("THREE", "999.67").replace("FOUR", "2");

Was This Post Helpful? 0
  • +
  • -

#3 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Replacing strings with numbers

Posted 11 April 2012 - 03:07 PM

Thanks mojo666,
My compiler says there's no matching function call for str_replace when it has just search and replace strings. Also, I was hoping, is there a way I could do it without rewriting variable values as strings (easier than creating a stringstream for each, that is)?
Thanks,
Zel2008

EDIT: I managed to get things working with replace after doing some more reading, but it still seems like using 4 lines to convert from a number to string is overkill. I suppose that I could be happy with the code I have, but if anyone knows how to improve it, I'd appreciate any ideas.

Here's the code, hopefully it will help someone:
#include <iostream>
#include <sstream>
using namespace std;

int main() {
	string temp = "some stuff ONE more TWO even more stuff THREE stuff FOUR";
	stringstream convert( stringstream::in | stringstream::out );

	int one = 5;
	convert.str( "" );
	convert << one;
	string oneStr = convert.str();

	double two = 77.14;
	convert.str( "" );
	convert << two;
	string twoStr = convert.str();

	double three = 999.67;
	convert.str( "" );
	convert << three;
	string threeStr = convert.str();

	int four = 11111;
	convert.str( "" );
	convert << four;
	string fourStr = convert.str();

	cout << temp << endl;
	temp = temp.replace( temp.find( "ONE" ), 3, oneStr );
	temp = temp.replace( temp.find( "TWO" ), 3, twoStr );
	temp = temp.replace( temp.find( "THREE" ), 5, threeStr );
	temp = temp.replace( temp.find( "FOUR" ), 4, fourStr );
	cout << temp << endl;
}


This post has been edited by Zel2008: 11 April 2012 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#4 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Replacing strings with numbers

Posted 11 April 2012 - 03:39 PM

Hi again everybody,
I'm not sure if this is going to show up as part of my previous post or not, but if it does sorry for the confusion. Anyway, I just stumbled on a really weird syntax that apparently can do all this in one line:

#include <iostream>
#include <ostream>
#include <sstream>
using namespace std;

int main() {
	string temp = "some stuff ONE more TWO even more stuff THREE stuff FOUR FIVE";
	stringstream convert( stringstream::in | stringstream::out );

	int one = 5;
	convert.str( "" );
	convert << one;
	string oneStr = convert.str();

	double two = 77.14;
	convert.str( "" );
	convert << two;
	string twoStr = convert.str();

	double three = 999.67;
	convert.str( "" );
	convert << three;
	string threeStr = convert.str();

	int four = 11111;
	convert.str( "" );
	convert << four;
	string fourStr = convert.str();

	double five = 77.37;
	string fiveStr = static_cast<ostringstream*>( &(ostringstream() << five) )->str();  // <- weird syntax!

	cout << temp << endl;
	temp = temp.replace( temp.find( "ONE" ), 3, oneStr );
	temp = temp.replace( temp.find( "TWO" ), 3, twoStr );
	temp = temp.replace( temp.find( "THREE" ), 5, threeStr );
	temp = temp.replace( temp.find( "FOUR" ), 4, fourStr );
	temp = temp.replace( temp.find( "FIVE" ), 4, fiveStr );
	cout << temp << endl;
}



I've never seen that syntax before, but there doesn't appear to be any major cons against it. Does anyone know more about it, maybe some cons I'm not aware of?

Thanks,
Zel2008
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1