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_ty pe begIndex, endIndex;
begIndex = src.find_first_ of(delims);
while (begIndex != string::npos)
{
if (src[begIndex] == '&')
{
src.replace(beg Index,begIndex, "&");
begIndex += 5;
}
else if (src[begIndex] == '<')
{
src.replace(beg Index,begIndex, "<");
begIndex += 4;
}
cout <<src <<endl;
begIndex = src.find_first_ of(delims, begIndex);
}
}
int main ()
{
string charCont = "a&b<c";
int cap = charCont.capaci ty();
int size = charCont.size() ;
cout << "capacity: "<<cap << " " <<"size: " <<size <<endl;
charEscape (charCont);
cout << charCont <<endl;
cap = charCont.capaci ty();
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 4510
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_ty pe begIndex, endIndex;
begIndex = src.find_first_ of(delims); while (begIndex != string::npos) { if (src[begIndex] == '&') { src.replace(beg Index,begIndex, "&");
src.replace(beg Index, 1, "&"); begIndex += 5; } else if (src[begIndex] == '<') { src.replace(beg Index,begIndex, "<");
src.replace(beg Index, 1, "<"); begIndex += 4; }
cout <<src <<endl; begIndex = src.find_first_ of(delims, begIndex); } }
int main () { string charCont = "a&b<c"; int cap = charCont.capaci ty(); int size = charCont.size() ; cout << "capacity: "<<cap << " " <<"size: " <<size <<endl; charEscape (charCont);
cout << charCont <<endl; cap = charCont.capaci ty(); 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::re place() 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::r eplace.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
hi! src.replace(beg Index,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(beg Index, 1, "&whatever" );
and it should work as expected.
regards,
sev
baobaoba wrote in news:25******** *************** ***@posting.goo gle.com:
[snip] while (begIndex != string::npos) { if (src[begIndex] == '&') { src.replace(beg Index,begIndex, "&");
src.replace(beg Index,1, "&");
begIndex += 5; } else if (src[begIndex] == '<') { src.replace(beg Index,begIndex, "<");
src.replace(beg Index,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 information.
>>> import re
>>> re.split(r'\W+', 'a b c d')
>>> # Expected result.
|
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 pointer which cannot be used
with strlwr() as it does the conversion inplace. So, I use the
following logic of copying the contents to a dynamically allocated
char* array and then doing the conversion:
-----------------------------
|
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 am lazy, hence I
am YYY"
str = replace(str, "YYY", "zzzzing"); // str now is "I am lazy, hence 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 below would work...
string gsub(const string & sData,
const string & sFrom,
| |
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 find one.
|
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 StringBuilder. At
present I am
porting a VB6 webclass app to VB.NET and therefore I am trying to make it as
efficent as possible via the new functionality of VB.NET.
|
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 brackets are not regular expression syntax.
Thanks,
|
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 - 5 ) * (10200 - 9000)
else 0 End If"
How do I evaluate/execute the string as code and return the answer, in
this case: 1200?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |