replacing a part of a string

This is a discussion on replacing a part of a string within the C++ Programming forums, part of the General Programming Boards category; Hello folks.. Can someone suggest me a way of replacing a part of a string with something else? What I ...

  1. #1
    Unregistered
    Guest

    Lightbulb replacing a part of a string

    Hello folks.. Can someone suggest me a way of replacing a part of a string with something else? What I mean is:
    Code:
    int main()
    {
    string someString("My name is michael");
    replace(someString,"michael","alex");
    return 0;
    }
    Here I want to know if there is a generic way of doing this. Any help will appreciated. Thanks

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    no, I think that you would have to create an array of chars and replace them manually according to the index number of each char...

  3. #3
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Have a look around for some examples of substr, there are quite a few on the web, it needs to know the start position of the part of the string you want to change, and the length of the string. "The C++ Programming language" gives the example...

    Code:
    string name = "Niels Stroustrup";
    
    void m3()
    {
    string s = name.substr(6,10);
    name.replace (0,5,"Nicholas");
    }
    [edit]
    This replaces Niels with Nicholas
    [/edit]
    Last edited by Azuth; 05-20-2002 at 04:35 AM.
    Demonographic rhinology is not the only possible outcome, but why take the chance

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, 10:20 AM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

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