replace special characters in string

Discussion in 'C++' started by mr h q, Nov 24, 2003.

  1. mr h q

    mr h q Guest

    Hi all,

    i want to replace $ to \$ so linux can work with paths and filenames
    that contain $.
    I wrote the following code

    for(string::size_type i = s.find(exist, 0); i != string::npos; i =
    s.find(foo, i))
    {
    s.replace(i, foo.size(), bar);
    }

    It's logical that this creates a loop that never ends, because i don't
    replace the $, so my question is how to replace $ with \$ and then
    jump to the next, instead of replacing \$ again..

    tia...
     
    mr h q, Nov 24, 2003
    #1
    1. Advertising

  2. mr h q wrote:
    >
    > Hi all,
    >
    > i want to replace $ to \$ so linux can work with paths and filenames
    > that contain $.
    > I wrote the following code
    >
    > for(string::size_type i = s.find(exist, 0); i != string::npos; i =
    > s.find(foo, i))
    > {
    > s.replace(i, foo.size(), bar);
    > }
    >
    > It's logical that this creates a loop that never ends, because i don't
    > replace the $, so my question is how to replace $ with \$ and then
    > jump to the next, instead of replacing \$ again..


    Well. If the $ happend to be at eg position 5 and you replace
    $ with \$, then you know that the $ will move to position 6. Thus
    if you continue searching at position 7, you will not interfere
    with the text you replaced :)

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Nov 24, 2003
    #2
    1. Advertising

  3. mr h q

    Rolf Magnus Guest

    mr h q wrote:

    > Hi all,
    >
    > i want to replace $ to \$ so linux can work with paths and filenames
    > that contain $.
    > I wrote the following code
    >
    > for(string::size_type i = s.find(exist, 0); i != string::npos; i =
    > s.find(foo, i))
    > {
    > s.replace(i, foo.size(), bar);
    > }
    >
    > It's logical that this creates a loop that never ends, because i don't
    > replace the $, so my question is how to replace $ with \$ and then
    > jump to the next, instead of replacing \$ again..


    You have to start your following find() calls _after_ the replaced
    string, not at the same location again. So try something like:

    for(string::size_type i = s.find(exist, 0); i != string::npos;
    i = s.find(foo, i + bar.size()))
    {
    s.replace(i, foo.size(), bar);
    }

    This should be save, since after the replacement, the string will be at
    least i + bar.size() long.
     
    Rolf Magnus, Nov 24, 2003
    #3
  4. mr h q

    Brad Marts Guest

    On Mon, 24 Nov 2003 15:53:52 +0100, Karl Heinz Buchegger wrote:


    > mr h q wrote:
    >>
    >> Hi all,
    >>
    >> i want to replace $ to \$ so linux can work with paths and filenames


    > Well. If the $ happend to be at eg position 5 and you replace
    > $ with \$, then you know that the $ will move to position 6. Thus
    > if you continue searching at position 7, you will not interfere
    > with the text you replaced :)


    In this case wouldn't the character that was originally at position 6 be
    overwritten with $? I think he also needs to be careful to copy everything
    after the $ to one position later in the string.
     
    Brad Marts, Nov 24, 2003
    #4
  5. mr h q

    mr h q Guest

    Rolf Magnus <> wrote in message news:<bpt8um$cjg$03$-online.com>...
    > mr h q wrote:
    >
    > > Hi all,
    > >
    > > i want to replace $ to \$ so linux can work with paths and filenames
    > > that contain $.
    > > I wrote the following code
    > >
    > > for(string::size_type i = s.find(exist, 0); i != string::npos; i =
    > > s.find(foo, i))
    > > {
    > > s.replace(i, foo.size(), bar);
    > > }
    > >
    > > It's logical that this creates a loop that never ends, because i don't
    > > replace the $, so my question is how to replace $ with \$ and then
    > > jump to the next, instead of replacing \$ again..

    >
    > You have to start your following find() calls _after_ the replaced
    > string, not at the same location again. So try something like:
    >
    > for(string::size_type i = s.find(exist, 0); i != string::npos;
    > i = s.find(foo, i + bar.size()))
    > {
    > s.replace(i, foo.size(), bar);
    > }
    >
    > This should be save, since after the replacement, the string will be at
    > least i + bar.size() long.



    It works now...thanks!!
     
    mr h q, Nov 25, 2003
    #5
  6. Brad Marts wrote:
    >
    > On Mon, 24 Nov 2003 15:53:52 +0100, Karl Heinz Buchegger wrote:
    >
    > > mr h q wrote:
    > >>
    > >> Hi all,
    > >>
    > >> i want to replace $ to \$ so linux can work with paths and filenames

    >
    > > Well. If the $ happend to be at eg position 5 and you replace
    > > $ with \$, then you know that the $ will move to position 6. Thus
    > > if you continue searching at position 7, you will not interfere
    > > with the text you replaced :)

    >
    > In this case wouldn't the character that was originally at position 6 be
    > overwritten with $?


    No, the test is *inserted*, meaning all text right to that position
    gets shifted to the right.

    If you replace $ with \$ in
    abcd$efg

    you get
    abcd\$efg

    The string got longer!

    > I think he also needs to be careful to copy everything
    > after the $ to one position later in the string.


    std::string.replace does that already.
    The OP's question turns around the find function used to find the next occurence
    of the text to replace:

    abc$def$ghi

    find finds the first $ at position 3. Replacing that with the replacement text
    leads to

    abc\$def$ghi

    Now if the find is restarted at position 4, it immediatly will find another $
    at position 4. Doing the replacement leads to

    abc\\$def$ghi

    and so on and so on. The solution of course is to start the next find at the
    end of the replaced text.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Nov 25, 2003
    #6
    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. Stefan Mueller
    Replies:
    3
    Views:
    33,136
    Stefan Mueller
    Jul 23, 2006
  2. Replies:
    1
    Views:
    8,028
    Martin Honnen
    Apr 26, 2005
  3. John_Woo
    Replies:
    4
    Views:
    673
    shakah
    Jul 6, 2006
  4. Joseph Kesselman

    Re: replace special characters

    Joseph Kesselman, Jan 11, 2007, in forum: XML
    Replies:
    0
    Views:
    488
    Joseph Kesselman
    Jan 11, 2007
  5. Replies:
    0
    Views:
    508
Loading...

Share This Page