Is this behavior of string.replace(beg, beg, str) normal ?

Discussion in 'C++' started by baobaoba, Nov 18, 2003.

  1. baobaoba

    baobaoba Guest

    Hi, I have a piece of code to do string replacement :

    #include <iostream>
    #include <string>
    using namespace std;

    void charEscape (string& src)
    {
    const string delims("&<");
    string::size_type begIndex, endIndex;

    begIndex = src.find_first_of(delims);
    while (begIndex != string::npos)
    {
    if (src[begIndex] == '&')
    {
    src.replace(begIndex,begIndex, "&amp;");
    begIndex += 5;
    }
    else if (src[begIndex] == '<')
    {
    src.replace(begIndex,begIndex, "&lt;");
    begIndex += 4;
    }

    cout <<src <<endl;
    begIndex = src.find_first_of(delims, begIndex);
    }
    }

    int main ()
    {
    string charCont = "a&b<c";
    int cap = charCont.capacity();
    int size = charCont.size();
    cout << "capacity: "<<cap << " " <<"size: " <<size <<endl;
    charEscape (charCont);


    cout << charCont <<endl;
    cap = charCont.capacity();
    size = charCont.size();
    cout << "capacity: "<<cap << " " <<"size: " <<size <<endl;
    }




    I want to replace "&" with "&amp;" and "<" with "&lt;". What I got finally
    is "a&amp;b&lt;". 'c' was lost. I test this on VC++6 and g++3x with the
    same result. Can anybody point out what I did wrong ?

    thanks
     
    baobaoba, Nov 18, 2003
    #1
    1. Advertising

  2. baobaoba

    red floyd Guest

    baobaoba wrote:
    > Hi, I have a piece of code to do string replacement :
    >
    > #include <iostream>
    > #include <string>
    > using namespace std;
    >
    > void charEscape (string& src)
    > {
    > const string delims("&<");
    > string::size_type begIndex, endIndex;
    >
    > begIndex = src.find_first_of(delims);
    > while (begIndex != string::npos)
    > {
    > if (src[begIndex] == '&')
    > {
    > src.replace(begIndex,begIndex, "&amp;");

    src.replace(begIndex, 1, "&amp;");
    > begIndex += 5;
    > }
    > else if (src[begIndex] == '<')
    > {
    > src.replace(begIndex,begIndex, "&lt;");

    src.replace(begIndex, 1, "&lt;");
    > begIndex += 4;
    > }
    >
    > cout <<src <<endl;
    > begIndex = src.find_first_of(delims, begIndex);
    > }
    > }
    >
    > int main ()
    > {
    > string charCont = "a&b<c";
    > int cap = charCont.capacity();
    > int size = charCont.size();
    > cout << "capacity: "<<cap << " " <<"size: " <<size <<endl;
    > charEscape (charCont);
    >
    >
    > cout << charCont <<endl;
    > cap = charCont.capacity();
    > size = charCont.size();
    > cout << "capacity: "<<cap << " " <<"size: " <<size <<endl;
    > }
    >
    >
    >
    >
    > I want to replace "&" with "&amp;" and "<" with "&lt;". What I got finally
    > is "a&amp;b&lt;". 'c' was lost. I test this on VC++6 and g++3x with the
    > same result. Can anybody point out what I did wrong ?
    >

    The second one replaced 7 characters with "&lt", instead of the 1 you wanted.
    The second parameter to std::string::replace() is a count of the chars in the original
    string that you want replaced.
    > thanks
     
    red floyd, Nov 18, 2003
    #2
    1. Advertising

  3. baobaoba

    Pete Becker Guest

    baobaoba wrote:
    >
    > I want to replace "&" with "&amp;" and "<" with "&lt;". What I got finally
    > is "a&amp;b&lt;". 'c' was lost. I test this on VC++6 and g++3x with the
    > same result. Can anybody point out what I did wrong ?
    >


    Read about the second argument to basic_string::replace.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Nov 19, 2003
    #3
  4. hi!

    > src.replace(begIndex,begIndex, "&amp;");

    if replace is used with size_type parameters (param 1 and 2) instead of
    iterators it means that it replaces up to 2nd parameter beginning at the
    first parameter

    use

    src.replace(begIndex, 1, "&whatever");

    and it should work as expected.

    regards,
    sev
     
    Severin Ecker, Nov 19, 2003
    #4
  5. baobaoba wrote in news::

    [snip]

    > while (begIndex != string::npos)
    > {
    > if (src[begIndex] == '&')
    > {
    > src.replace(begIndex,begIndex, "&amp;");


    src.replace(begIndex,1, "&amp;");


    > begIndex += 5;
    > }
    > else if (src[begIndex] == '<')
    > {
    > src.replace(begIndex,begIndex, "&lt;");


    src.replace(begIndex,1, "&lt;");

    > begIndex += 4;
    > }
    >
    > cout <<src <<endl;
    > begIndex = src.find_first_of(delims, begIndex);
    > }
    > }
    >


    [snip]

    >
    > I want to replace "&" with "&amp;" and "<" with "&lt;". What I got
    > finally is "a&amp;b&lt;". 'c' was lost. I test this on VC++6 and g++3x
    > with the same result. Can anybody point out what I did wrong ?
    >


    string.replace( offset, count, value );


    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Nov 19, 2003
    #5
    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. =?Utf-8?B?amhpbGw=?=
    Replies:
    0
    Views:
    376
    =?Utf-8?B?amhpbGw=?=
    May 13, 2004
  2. David
    Replies:
    2
    Views:
    480
    Thomas G. Marshall
    Aug 3, 2003
  3. mchoya
    Replies:
    4
    Views:
    322
    Thomas Matthews
    Aug 30, 2004
  4. nafai3000
    Replies:
    1
    Views:
    486
    CrayzeeWulf
    Apr 25, 2005
  5. Walle Wallen
    Replies:
    3
    Views:
    91
    Walle Wallen
    Apr 28, 2010
Loading...

Share This Page