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, "&");
begIndex += 5;
}
else if (src[begIndex] == '<')
{
src.replace(begIndex,begIndex, "<");
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 "&" and "<" with "<". What I got finally
is "a&b<". '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 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, "&");
src.replace(begIndex, 1, "&"); begIndex += 5; } else if (src[begIndex] == '<') { src.replace(begIndex,begIndex, "<");
src.replace(begIndex, 1, "<"); 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 "&" and "<" with "<". What I got finally is "a&b<". '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 "<", 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
baobaoba wrote: I want to replace "&" with "&" and "<" with "<". What I got finally is "a&b<". '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)
hi! src.replace(begIndex,begIndex, "&");
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
baobaoba wrote in news:25**************************@posting.google.c om:
[snip] while (begIndex != string::npos) { if (src[begIndex] == '&') { src.replace(begIndex,begIndex, "&");
src.replace(begIndex,1, "&");
begIndex += 5; } else if (src[begIndex] == '<') { src.replace(begIndex,begIndex, "<");
src.replace(begIndex,1, "<");
begIndex += 4; }
cout <<src <<endl; begIndex = src.find_first_of(delims, begIndex); } }
[snip] I want to replace "&" with "&" and "<" with "<". What I got finally is "a&b<". '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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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 -...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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
|
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...
| | |