Remove Data from basic::string

M

Mike Copeland

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
 
S

Stefan Ram

basic::string myString = "123 E, Main Street";
and I wish to remove (delete) the "," from it.

The details depends on missing parts of the problem
specification. For example, to delete the »,« at compile time,
change the source code to

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

. A function to delete the »,« at run-time depends on the
range of permissible values for the specification of the
string and the text to be deleted and the intended result or
effect.

I think the best way to pose such a question is to write a
function declaration (but not a function definition) plus
documentation for the function and then to ask for the
implementation. It would even be better if a test client
would be supplied by you, too.
 
T

Tim Love

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
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());
 
P

Pascal J. Bourguignon

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

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;
}
 
G

Gernot Frisch

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.
 
J

James Kanze

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());

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() ) ;
 
J

James Kanze

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;
}

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?)
 
M

Mike Copeland

basic::string myString = "123 E, Main Street";
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top