how can I replace a substring in a string

Discussion in 'C++' started by silverburgh.meryl@gmail.com, May 30, 2007.

  1. Guest

    Hi,

    If I have a string like this:
    char buff[10];
    buff[0] ='h';
    buff[1] ='e';
    buff[2] ='l';
    buff[3] ='l';
    buff[4] ='o';

    string s(buff);

    How can i replace 'll' with 'abc'
    and what if I replace 'll' with 'a', will the 'o' move up by 1 index?

    Thank you.
     
    , May 30, 2007
    #1
    1. Advertising

  2. skrev:
    > Hi,
    >
    > If I have a string like this:
    > char buff[10];
    > buff[0] ='h';
    > buff[1] ='e';
    > buff[2] ='l';
    > buff[3] ='l';
    > buff[4] ='o';
    >


    buff[5] = 0;

    > string s(buff);
    >
    > How can i replace 'll' with 'abc'
    > and what if I replace 'll' with 'a', will the 'o' move up by 1 index?
    >


    replace(s, "ll", "abc");

    void replace(std::string &target, std::string &that, std::string &with) {
    std::string::size_type where = target.find(that);
    if(where != std::string::npos) {
    target.replace(target.begin() + where,
    target.begin() + where + that.size(),
    with.begin(),
    with.end());
    }
    }

    --
    OU
     
    Obnoxious User, May 30, 2007
    #2
    1. Advertising

  3. On May 30, 10:04 am, ""
    <> wrote:
    > Hi,
    >
    > If I have a string like this:
    > char buff[10];
    > buff[0] ='h';
    > buff[1] ='e';
    > buff[2] ='l';
    > buff[3] ='l';
    > buff[4] ='o';
    >

    You need to insert a null character at postion 5.

    > string s(buff);
    >
    > How can i replace 'll' with 'abc'

    s.replace(2,2,"abc");
    > and what if I replace 'll' with 'a', will the 'o' move up by 1 index?

    yes
    >
    > Thank you.
     
    Naresh Rautela, May 30, 2007
    #3
  4. Devon Null Guest

    wrote:
    > Hi,
    >
    > If I have a string like this:
    > char buff[10];
    > buff[0] ='h';
    > buff[1] ='e';
    > buff[2] ='l';
    > buff[3] ='l';
    > buff[4] ='o';
    >
    > string s(buff);
    >
    > How can i replace 'll' with 'abc'
    > and what if I replace 'll' with 'a', will the 'o' move up by 1 index?
    >
    > Thank you.
    >


    Here are all the string class member functions:

    http://cppreference.com/cppstring/index.html
     
    Devon Null, May 30, 2007
    #4
  5. On Wed, 30 May 2007 19:30:52 +0200, Obnoxious User wrote:
    [...]
    >void replace(std::string &target, std::string &that, std::string &with) {


    const correctness?


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
     
    Roland Pibinger, May 30, 2007
    #5
  6. Roland Pibinger skrev:
    > On Wed, 30 May 2007 19:30:52 +0200, Obnoxious User wrote:
    > [...]
    >> void replace(std::string &target, std::string &that, std::string &with) {

    >
    > const correctness?
    >
    >


    void replace(std::string & target,
    std::string const & that,
    std::string const & with);

    --
    OU
     
    Obnoxious User, May 30, 2007
    #6
  7. <> wrote in message news:...

    > Hi,
    >
    > If I have a string like this:
    > char buff[10];
    > buff[0] ='h';
    > buff[1] ='e';
    > buff[2] ='l';
    > buff[3] ='l';
    > buff[4] ='o';


    Triply wrong:
    1. Firstly, null-terminated strings need to be, well, null-terminated.
    So you'd need to set:
    buff[5] = '\0';
    2. Why not just initialize the buffer to the string?
    char buff[10] = "hello";
    3. Why use a char[] buffer at all? Just do this:
    std::string MyString ("hello");

    > string s(buff);


    std::string

    > How can i replace 'll' with 'abc'


    Like so (tested, working program; compile this for demo):

    #include <iostream>
    #include <string>
    int main (void)
    {
    std::string S ("hello");
    S.replace(S.find("ll"), 2, "abc"); // replace 2 chars at "ll" by "abc"
    std::cout << S << std::endl;
    return 0;
    }

    > and what if I replace 'll' with 'a', will the 'o' move up by 1 index?


    It decrements by one, from 4 to 3.

    > Thank you.


    You're welcome.

    --
    Cheers,
    Robbie Hatley
    lone wolf aatt well dott com
    triple-dubya dott Tustin Free Zone dott org
     
    Robbie Hatley, May 31, 2007
    #7
  8. "Obnoxious User" <OU@127.0.0.1> wrote in message
    news:465db294$0$20045$...

    > replace(s, "ll", "abc");
    >
    > void replace(std::string &target, std::string &that, std::string &with) {
    > std::string::size_type where = target.find(that);
    > if(where != std::string::npos) {
    > target.replace(target.begin() + where,
    > target.begin() + where + that.size(),
    > with.begin(),
    > with.end());
    > }
    > }


    Ewww. I could replace all of the above with just this:

    s.replace(s.find("ll"), 2, "abc");

    or more generically:

    s.replace(s.find(target), target_size, replacement);

    Far more convenient, and far less prone to error. "find" and "replace" are
    already built into the std::string class, so might as well use them.

    --
    Cheers,
    Robbie Hatley
    lone wolf aatt well dott com
    triple-dubya dott Tustin Free Zone dott org
     
    Robbie Hatley, May 31, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dominique Deleris
    Replies:
    4
    Views:
    498
  2. Paul
    Replies:
    19
    Views:
    13,243
    Netocrat
    Nov 10, 2005
  3. magix

    replace substring

    magix, May 26, 2008, in forum: C Programming
    Replies:
    20
    Views:
    968
    Flash Gordon
    Jun 6, 2008
  4. Replies:
    3
    Views:
    235
    Sherm Pendley
    Aug 3, 2005
  5. SM
    Replies:
    4
    Views:
    229
Loading...

Share This Page