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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • baobaoba

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

    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_ty pe begIndex, endIndex;

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

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

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


    cout << charCont <<endl;
    cap = charCont.capaci ty();
    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
  • red floyd

    #2
    Re: Is this behavior of string.replace( beg, beg, str) normal ?

    baobaoba wrote:[color=blue]
    > 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_ty pe begIndex, endIndex;
    >
    > begIndex = src.find_first_ of(delims);
    > while (begIndex != string::npos)
    > {
    > if (src[begIndex] == '&')
    > {
    > src.replace(beg Index,begIndex, "&amp;");[/color]
    src.replace(beg Index, 1, "&amp;");[color=blue]
    > begIndex += 5;
    > }
    > else if (src[begIndex] == '<')
    > {
    > src.replace(beg Index,begIndex, "&lt;");[/color]
    src.replace(beg Index, 1, "&lt;");[color=blue]
    > begIndex += 4;
    > }
    >
    > cout <<src <<endl;
    > begIndex = src.find_first_ of(delims, begIndex);
    > }
    > }
    >
    > int main ()
    > {
    > string charCont = "a&b<c";
    > int cap = charCont.capaci ty();
    > int size = charCont.size() ;
    > cout << "capacity: "<<cap << " " <<"size: " <<size <<endl;
    > charEscape (charCont);
    >
    >
    > cout << charCont <<endl;
    > cap = charCont.capaci ty();
    > 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 ?
    >[/color]
    The second one replaced 7 characters with "&lt", instead of the 1 you wanted.
    The second parameter to std::string::re place() is a count of the chars in the original
    string that you want replaced.[color=blue]
    > thanks[/color]

    Comment

    • Pete Becker

      #3
      Re: Is this behavior of string.replace( beg, beg, str) normal ?

      baobaoba wrote:[color=blue]
      >
      > 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 ?
      >[/color]

      Read about the second argument to basic_string::r eplace.

      --

      Pete Becker
      Dinkumware, Ltd. (http://www.dinkumware.com)

      Comment

      • Severin Ecker

        #4
        Re: Is this behavior of string.replace( beg, beg, str) normal ?

        hi!
        [color=blue]
        > src.replace(beg Index,begIndex, "&amp;");[/color]
        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(beg Index, 1, "&whatever" );

        and it should work as expected.

        regards,
        sev


        Comment

        • Rob Williscroft

          #5
          Re: Is this behavior of string.replace( beg, beg, str) normal ?

          baobaoba wrote in news:25212a12.0 311181545.63e69 944@posting.goo gle.com:

          [snip]
          [color=blue]
          > while (begIndex != string::npos)
          > {
          > if (src[begIndex] == '&')
          > {
          > src.replace(beg Index,begIndex, "&amp;");[/color]

          src.replace(beg Index,1, "&amp;");

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

          src.replace(beg Index,1, "&lt;");
          [color=blue]
          > begIndex += 4;
          > }
          >
          > cout <<src <<endl;
          > begIndex = src.find_first_ of(delims, begIndex);
          > }
          > }
          >[/color]

          [snip]
          [color=blue]
          >
          > 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 ?
          >[/color]

          string.replace( offset, count, value );


          HTH

          Rob.
          --

          Comment

          Working...