4 Replies - 301 Views - Last Post: 05 December 2012 - 12:29 PM Rate Topic: -----

#1 apaddobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 16-May 12

Replacing part of a string with another string

Posted 04 December 2012 - 08:06 PM

Hi

I have the following code with me that works.

#include <iostream>
#include <string>
using namespace std;

main ()
{
string s = "key1=value&key2=value2&key3=value3";

cout << "Original : "<<s.c_str () <<endl;

string s1 = "key2";

	int pos;
	pos = s.find (s1, 0);

string s3 (s, 0, pos+s1.size () + 1);
s3 += "newvalue";

string r_s = s;
r_s.replace (0, pos+s1.size ()+1, "");

const char* ptr = strtok (const_cast<char*>(r_s.c_str ()), "&");

string full_replace = s1;
full_replace += "=";
string ptr_s (ptr);
full_replace += ptr_s;

int loc = s.find (full_replace);

string s2 = s;
if (loc != string::npos) 
    s2.replace (0, loc + full_replace.size (), "");

s3 += s2;
cout << "New  : "<<s3.c_str ()<<endl;
}





./a.out
Original : key1=value&key2=value2&key3=value3
New : key1=value&key2=newvalue&key3=value3

You can see the value2 is replaced with newvalue retaining the rest of the string as is.

I just have a feeling this isn't a very smart way of achieving this. Can someone suggest a better way of doing this? Efficient and faster, if possible.

Is This A Good Question/Topic? 0
  • +

Replies To: Replacing part of a string with another string

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2984
  • Posts: 10,320
  • Joined: 08-August 08

Re: Replacing part of a string with another string

Posted 04 December 2012 - 08:50 PM

You could use replace.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2984
  • Posts: 10,320
  • Joined: 08-August 08

Re: Replacing part of a string with another string

Posted 05 December 2012 - 10:40 AM

Oops! Looking again I see that you are using it. I suggest you try using find_first_of and find_first_not_of.

You could use an array of strings to hold the keys and write a function to find the value for a given key. Then it's just a matter of looping through the keys and passing them to the function to get your values.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2984
  • Posts: 10,320
  • Joined: 08-August 08

Re: Replacing part of a string with another string

Posted 05 December 2012 - 11:25 AM

Hmm, it looks like I messed this up, so here's my solution:
#include <iostream>
#include <string>

using namespace std;

string get_value(string &key, string str);
string new_value(string key[], string val[], string new_v);

int main()
{
	string keys[3] = {"key1","key2","key3"};
	string s = "key1=first&key2=second&key3=third";
	string newval = "newvalue";
	string newstring[3];
	for (int i = 0; i< 3; i++) {
		newstring[i] = get_value(keys[i], s);
		cout << "*"<< newstring[i] <<"*"<< endl;
	}
	cout	<< new_value(keys, newstring, newval)<< endl;
	
	return 0;
}

string new_value(string key[], string val[], string new_v) {
	return  key[0] + "=" + val[0] + "&" + key[1] + "=" + new_v +"&" + key[2] + "=" + val[2];

}

string get_value(string &key, string str) {
	string value;
	long p0, p1;
	p0 = str.find(key)+key.length()+1;
	p1 = str.find_first_of("&",p0);
	//p1 = str.find("&",p0); // Could use either find or find_first_of
	value = str.substr(p0,p1-p0);
	return value;
}


The new_value() function could be improved.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5829
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Replacing part of a string with another string

Posted 05 December 2012 - 12:29 PM

Just as an alternate...
string &setKey(string &s, const string &key, const string &value) {
	string keyFind(key + "=");
	istringstream ssi( s );
	ostringstream sso;

	bool firstPair = true, found = false;
	while (!ssi.eof()) {
		if (!firstPair) { sso << '&'; } else { firstPair = false; }
		string strPair;
		getline( ssi, strPair, '&' );
		if (strPair.find(keyFind)==0) {
			if (!found) { // ignore dups
				sso << key << '=' << value;
				found = true;
			}
		} else {
			sso << strPair;
		}
	}
	if (!found) { // add keys
		if (!firstPair) { sso << '&'; }
		sso << key << '=' << value;
	}
	s = sso.str();
	return s;
}

// test code
string s("key1=value&key2=value2&key3=value3");

cout << s << endl;
cout << setKey(s, "key2", "nv2") << endl;
cout << setKey(s, "key3", "nv3") << endl;
cout << setKey(s, "key5", "nv5") << endl;
cout << setKey(s, "key1", "nv1") << endl;



Perhaps a little much, but fun. Importantly, it's hard to fake out.

Though, honestly, this is what you want a map for. If order or duplicates are important, then a vector of pair.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1