Is it Posible to do something like the following with a string:
Find all the word "hello" in the string and replace them with "bye"
Is it Posible to do something like the following with a string:
Find all the word "hello" in the string and replace them with "bye"
Vista - Something We all Love to Hate.
Yes.
Perfect answer, lol. Short and to the point, eh cyberfish?
Code:void find_and_replace(std::string& source, std::string const& find, std::string const& replace) { for(std::string::size_type i = 0; (i = source.find(find, i)) != std::string::npos;) { source.replace(i, find.length(), replace); i += replace.length() - find.length() + 1; } } std::string s("I would like to say hello"); find_and_replace(s, "hello", "bye");
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
Yes, because s/he hasn't demonstrated any effort to trying to find an answer him/herself. A simple Google search will probably turn up 10 repetitions of the same answer in a few milliseconds.Perfect answer, lol. Short and to the point, eh cyberfish?
replace [C++ Reference]
http://catb.org/esr/faqs/smart-questions.html
Believe me Ive searched
Vista - Something We all Love to Hate.
What search terms did you use?
How about "C++ string replace"?
I cut him some slack on this one, because I remember having issues with it.
A) The documentation explains how to replace if you know the positions in the string, or the first occurrence, and usually doesn't include an explanation on how to use the iterators to replace all occurrences.
B) I ran into a few flawed search and replace functions which didn't take into account the length changes when you replace previous occurrences, and resulted in infinite loops.
I didn't actually get a chance to test mine very much. Someone quickly pasting their own known working string search and replace function might go a long way.
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101