Connecting Tech Pros Worldwide Forums | Help | Site Map

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

baobaoba
Guest
 
Posts: n/a
#1: Jul 22 '05
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

red floyd
Guest
 
Posts: n/a
#2: Jul 22 '05

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_type begIndex, endIndex;
>
> begIndex = src.find_first_of(delims);
> while (begIndex != string::npos)
> {
> if (src[begIndex] == '&')
> {
> src.replace(begIndex,begIndex, "&amp;");[/color]
src.replace(begIndex, 1, "&amp;");[color=blue]
> begIndex += 5;
> }
> else if (src[begIndex] == '<')
> {
> src.replace(begIndex,begIndex, "&lt;");[/color]
src.replace(begIndex, 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.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 ?
>[/color]
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.[color=blue]
> thanks[/color]

Pete Becker
Guest
 
Posts: n/a
#3: Jul 22 '05

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::replace.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Severin Ecker
Guest
 
Posts: n/a
#4: Jul 22 '05

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


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

and it should work as expected.

regards,
sev


Rob Williscroft
Guest
 
Posts: n/a
#5: Jul 22 '05

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


baobaoba wrote in news:25212a12.0311181545.63e69944@posting.google.c om:

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

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

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

src.replace(begIndex,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.
--
http://www.victim-prime.dsl.pipex.com/
Closed Thread


Similar C / C++ bytes