0

Possible Duplicate:
C++: How do I replace all instances of of a string with another string?

Imagine you have a string: This is a string and this is the same string too and you want to replace every string word in the... well, in the string, and change it to chain in order to obtain: This is a chain and this is the same chain too.

The problem is that the function std::replace() only work nice if it changes one character for another (imagine... replace all e for i).

I found several solutions for that but all of them included other libraries or weird whiles (which I really don't understand).

What I want is an elegant solution for that using replace because I'm working in a project that times are everything.

Community
  • 1
  • 1
Urien
  • 33
  • 6
  • 1
    http://stackoverflow.com/questions/5343190/c-how-do-i-replace-all-instances-of-of-a-string-with-another-string – Mithun Sasidharan Nov 28 '11 at 13:10
  • There are a few gotchas. 1) What if the strings overlap? 2) What if the replacement string contains the original string? (e.g replace "This is a string" with "This is a string, too" – Steve C Nov 28 '11 at 13:14
  • A combination of `std::string::find` and `std::string::replace` should do the trick. – Kerrek SB Nov 28 '11 at 13:18

0 Answers0