?
Solved

std:replace

Posted on 2006-05-11
9
Medium Priority
?
6,206 Views
Last Modified: 2011-09-20
single char replace works
  replace( str.begin(), str.end(), 'a', 'b' );
but          
  replace( str.begin(), str.end(), "abc", "bcd" );
doesnt.

how does one replace all occourances of a string ?
0
Comment
Question by:ed987
9 Comments
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 1000 total points
ID: 16657210
Hi ed987,

You will have to programatically do that
You could use find() and use a while loop

Cheers!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16657245
You might want to use std::string::replace instead of std::replace
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16657254
>> how does one replace all occourances of a string ?
but to get all occurences, you'll have to combine that with std::string::find in a loop
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 53

Expert Comment

by:Infinity08
ID: 16657289
Something like this if I didn't make any mistakes :

    std::string orig = "abcdefghijabcdef";
    std::string old_str = "de";
    std::string new_str = "XYZ";
    std::string::size_type pos = 0;
    while (1) {
      pos = orig.find (old_str, pos);
      orig.replace (pos, old_str.length(), new_str);
      if (pos == std::string::npos) break;
    }

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16658810
>>>> Something like this if I didn't make any mistakes

Only two mistakes (not checking pos before replace, not incrementing pos) ;-)

string replaceAll(const string& input, const string& sold, const string& snew)
{
     string output = input;
     int pos = 0;
     int lpos = 0;
     while ((pos = output.find(sold, lpos)) != string::npos)
     {
           output.replace(pos, sold.length(), snew);
           lpos = pos+1;
     }
     return output;
}

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16658846
Oops, I made a mistake as well:          

>>>> lpos = pos+1;

must be

   lpos = pos+1 + snew.length();

or it is an infinite loop if the old string was part of the new string.

Regards, Alex

0
 
LVL 15

Expert Comment

by:efn
ID: 16659124
Shouldn't that be

   lpos = pos + snew.length();

?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16659205
>> Only two mistakes (not checking pos before replace, not incrementing pos) ;-)
oh-la-la lol
what was i thinking heh :)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16659804
>>>> Only two mistakes

Yes, I wouldn't like to have any advantage over you ;-)

>>>> lpos = pos + snew.length();

Yes, that is the ultimate solution ;-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question