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

B

baobaoba

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
 
R

red floyd

baobaoba said:
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.
 
P

Pete Becker

baobaoba said:
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.
 
S

Severin Ecker

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
 
R

Rob Williscroft

baobaoba wrote in
[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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top