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 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());
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__
Awesome! Thanks a lot for sharing, I never stumbled upon bind2nd() before.
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
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!