Strings Find and Replace

This is a discussion on Strings Find and Replace within the C++ Programming forums, part of the General Programming Boards category; Is it Posible to do something like the following with a string: Find all the word "hello" in the string ...

  1. #1
    Registered User kakayoma's Avatar
    Join Date
    Jul 2009
    Location
    Dallas, Texas
    Posts
    42

    Question Strings Find and Replace

    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.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Yes.

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by cyberfish View Post
    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

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Perfect answer, lol. Short and to the point, eh cyberfish?
    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.

    replace [C++ Reference]

    http://catb.org/esr/faqs/smart-questions.html

  5. #5
    Registered User kakayoma's Avatar
    Join Date
    Jul 2009
    Location
    Dallas, Texas
    Posts
    42

    Exclamation

    Believe me Ive searched

    Vista - Something We all Love to Hate.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    What search terms did you use?

    How about "C++ string replace"?

  7. #7
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by cyberfish View Post
    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.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find and replace Strings.
    By shabbirhussain in forum C Programming
    Replies: 2
    Last Post: 08-01-2008, 10:48 PM
  2. Replies: 10
    Last Post: 06-10-2008, 02:17 AM
  3. please help!...find and replace
    By amy589 in forum C++ Programming
    Replies: 26
    Last Post: 10-13-2006, 06:42 PM
  4. Replies: 5
    Last Post: 05-25-2004, 04:36 PM
  5. How to replace strings in a file?
    By Doh in forum C Programming
    Replies: 6
    Last Post: 11-26-2002, 11:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21