views:

130

answers:

3

Is it possible in C++ to replace part of a string with another string. Basically, I would like to do this

QString string("hello $name");
string.replace("$name", "Somename");

but I would like to use the Standard C++ libraries.

A: 

std::string has a replace method, is that what you are looking for?

You could try:

s.replace(s.find("$name"), sizeof("Somename")-1, "Somename");

I haven't tried myself, just read the documentation on find() and replace()

S.C. Madsen
From what I can see the std::string replace method doesn't take two strings as I would like.
TomMan
I think Michael Mrozek has a more elegant solution
S.C. Madsen
+2  A: 

Yes, you can do it, but you have to find the position of the first string with string's find() member, and then replace with it's replace() member.

string s("hello $name");
size_type pos = s.find( "$name" );
if ( pos != string::npos ) {
   s.replace( pos, 5, "somename" );   // 5 = length( $name )
}

If you are planning on using the Standard Library, you should really gt hold of a copy of the book The C++ Standard Library which covers all this stuff very well.

anon
Ok, I'll try and write my own function to do that.
TomMan
+6  A: 

There's a function to find a substring within a string (find), and a function to replace a particular range in a string with another string (replace), so you can combine those to get the effect you want:

bool replace(std::string& str, const std::string& from, const std::string& to) {
    size_t start_pos = string.find(from);
    if(start_pos == std::string::npos)
        return false;
    size_t end_pos = start_pos + from.length();
    str.replace(start_pos, end_pos, to);
    return true;
}

std::string string("hello $name");
replace(string, "$name", "Somename");

In response to a comment, I think replaceAll would probably look something like this:

void replaceAll(std::string& str, const std::string& from, const std::string& to) {
    size_t start_pos = 0;
    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
        size_t end_pos = start_pos + from.length();
        str.replace(start_pos, end_pos, to);
        start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
    }
}
Michael Mrozek
How would I fix it if the original string had more that one instance of "$name" and I wanted to replace all of them.
TomMan
Why aren't `from` and `to` passed per `const` reference? What does your function if `from` isn't there? `-1` from me for that.
sbi
Add a loop and use the "pos" argument of the find() method
S.C. Madsen
Ok, i'll try that. Thanks
TomMan
@sbi Fixed, although you could've phrased it as recommendations instead of attacks -- it simply didn't occur to me, I rarely think to use `const` and if I wrote a utility method like this I would only call it if I knew the replacement were valid
Michael Mrozek
+1: Good job Michael Mrozek
S.C. Madsen
@Michael: Good, I turned my down-vote into an up-vote. Dismissing `const` is disregarding one of C++' best tools. [Passing per `const` reference should be the default mode for function parameters.](http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c/2139254#2139254) (FTR, without the `const`, you couldn't even pass string literals to your function, because you cannot bind temporaries to non-`const` references. So the function wouldn't even do what it was written to.)
sbi