Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Remove Data from basic::string

0 views
Skip to first unread message

Mike Copeland

unread,
Apr 1, 2009, 10:41:05 AM4/1/09
to
What is a good way to remove a character from a basic::string?
For example, I have the following:

basic::string myString = "123 E, Main Street";

and I wish to remove (delete) the "," from it.
Does anyone know a way to do this? TIA

Message has been deleted

Tim Love

unread,
Apr 1, 2009, 10:51:17 AM4/1/09
to
mrc...@cox.net (Mike Copeland) writes:

I think the following will work (with a few includes, etc). Up to you whether you think it's a good way.


string::iterator new_end =remove_if(myString.begin(), myString.end(), bind2nd(equal_to<char>(), ','));
myString.erase(new_end, myString.end());

Pascal J. Bourguignon

unread,
Apr 1, 2009, 10:54:25 AM4/1/09
to
mrc...@cox.net (Mike Copeland) writes:

I don't know basic::string. Perhaps you can convert them to std::string?
Then you could do:

{
std::string temp=myString;
std::string::iterator it=temp.find(',');
if(it!=temp.end()){
temp.erase(it);
}
myString=temp;
}

--
__Pascal Bourguignon__

Vladyslav Lazarenko

unread,
Apr 1, 2009, 11:08:56 AM4/1/09
to

Gernot Frisch

unread,
Apr 1, 2009, 11:27:39 AM4/1/09
to

>
> string::iterator new_end =remove_if(myString.begin(), myString.end(),
> bind2nd(equal_to<char>(), ','));
> myString.erase(new_end, myString.end());

Awesome! Thanks a lot for sharing, I never stumbled upon bind2nd() before.

James Kanze

unread,
Apr 1, 2009, 3:50:14 PM4/1/09
to
On Apr 1, 4:51 pm, t...@eng.cam.ac.uk (Tim Love) wrote:

Isn't that doing things the hard way? There are times when
bind2nd and such are necessary, but the above does exactly the
same thing as:

myString.erase(
std::remove( myString.begin(), myString.end(), ',' ),
myString.end() ) ;

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

unread,
Apr 1, 2009, 3:54:15 PM4/1/09
to
On Apr 1, 4:54 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

Which raises an interesting question: is temp.erase(it)
guaranteed to work (i.e. do nothing) when it == temp.end()? I
would expect it to, but I can't figure out whether it is
guaranteed or not from the text in the standard.

(There are also a couple of questions concerning what he really
wants to do, of course. Remove the first comma? Remove all
commas?, remove some specific character that he has found by
other means?)

Mike Copeland

unread,
Apr 1, 2009, 7:38:16 PM4/1/09
to
> > > basic::string myString = "123 E, Main Street";
> > > and I wish to remove (delete) the "," from it.
> > > Does anyone know a way to do this? TIA
> > I don't know basic::string. Perhaps you can convert them to
> > std::string? Then you could do:
> > {
> > std::string temp=3DmyString;
> > std::string::iterator it = temp.find(',');
> > if(it!=3Dtemp.end()){
> > temp.erase(it);
> > }
> > myString=3Dtemp;

> > }
>
> Which raises an interesting question: is temp.erase(it)
> guaranteed to work (i.e. do nothing) when it == temp.end()? I
> would expect it to, but I can't figure out whether it is
> guaranteed or not from the text in the standard.
>
> (There are also a couple of questions concerning what he really
> wants to do, of course. Remove the first comma? Remove all
> commas?, remove some specific character that he has found by
> other means?)

FWIW, I want to remove/delete _all_ the commas in the string. My
example didn't go into that, because I wasn't aware of any STL function
that would delete a specific character from a string of characters. The
myString.erase() function accomplishes what I need (but didn't know
about), and I was able to modify the code to process multiples. I
tested it with most pathological situations (e.g. comma in first and
last positions), and it seems to do what I need.
Thanks to everyone who contributed!

0 new messages