473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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_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
4 4480
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robin Munn | last post by:
How is re.split supposed to work? This wasn't at all what I expected: $ python Python 2.2.2 (#1, Jan 12 2003, 12:07:20) on linux2 Type "help", "copyright", "credits" or "license" for more...
37
by: Zombie | last post by:
Hi, what is the correct way of converting contents of a <string> to lowercase? There are no methods of <string> class to do this so I fallback on strlwr(). But the c_str() method returns a const...
4
by: anhtt | last post by:
Hi, I am a newbie in C++. I wonder if there is a standard lib function do this sort of thing: string str = " I am XXX, hence I am YYY"; str = replace(str, "XXX", "lazy"); // str now is " I...
24
by: Wim Roffal | last post by:
Is there a possibility to do a string replace in javascript without regular experessions. It feels like using a hammer to crash an egg. Wim
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
7
by: david | last post by:
I have searched existing posts and have not found an answer to this variation of an old question. I have the following string stored in a variable Dim str as String = "If 9000 < 10200 Then (6 -...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.