Thread: replacing a substring of a string with another string

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    replacing a substring of a string with another string

    How can I use string.replace() to do something like:

    Code:
    string str, str2, str3;
    str  = "an adequate amount of text";
    str2 = "amount of ";
    st3 = "";
    
    str.replace(str2, str3); // an adequate text
    
    //or
    str  = "an adequate amount of text";
    str3 = "[how did this get here?]";
    
    str.replace(str2, str3); // an adequate [how did this get here?] text
    From looking at the documentation, it seems that replace() will not replace more than the amount of text it's replacing.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    replace() has many forms. http://www.cplusplus.com/reference/s...g/replace.html

    I suspect the first one will suit your needs.
    Code:
    string& replace ( size_t pos1, size_t n1,   const string& str );
    For example, this should print 1cat4.
    Code:
    string original = "1234";
    string replace = "cat";
    // replace original[1] .. original[2] ("23") with "cat"
    original.replace(1, 2, replace);
    cout << original;
    Hopefully I thought that through correctly . . . and hopefully I've answered your question.

    [edit] In your first case, maybe you want something like this.
    Code:
    string str, str2, str3;
    str  = "an adequate amount of text";
    str2 = "amount of ";
    st3 = "";
    
    while(1) {
        // search for an occurance of str2 in str
        size_t found = str.find(str2);
    
        // if such a substr was found,
        if(found != string::npos) {
            // replace it with str3
            str.replace(found, str2.length(), str3);
        }
        else break;  // if the substring wasn't found, exit the loop
    }
    See also http://www.cplusplus.com/reference/s...ring/find.html
    [/edit]
    Last edited by dwks; 10-14-2008 at 05:11 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to find str2 in str before calling replace. The find function will return an index which can be passed to replace (along with the length of str2) to indicate the location of the substring to be removed.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Ah, thank you. I read the example wrong, and it looked like replace(p, n, str) only replaced a maximum on n bytes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM