By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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


Jul 22 '05 #2

P: n/a
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)
Jul 22 '05 #3

P: n/a
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
Jul 22 '05 #4

P: n/a
baobaoba wrote in news:25**************************@posting.google.c om:

[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/
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.