Topic: remove characters from a string


Author: "seawind" <wangdongyu3d@hotmail.com>
Date: Tue, 29 Aug 2006 10:40:49 CST
Raw View
I am a newer, and now i encounter a simple question.

i want to erase all specified characters ,such as ',' , from a string.
which function should i call?
for example:

string ls( "i ,am, a ,dog,");

thanks in advace

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Jens Theisen <jth02@arcor.de>
Date: Tue, 29 Aug 2006 14:16:43 CST
Raw View
===================================== MODERATOR'S COMMENT:

I have allowed this message through as a reply to a previous post, but material of this form is generally off-topic; please do not continue this thread unless topical material (i.e., material related to standardization of C++) is involved.


===================================== END OF MODERATOR'S COMMENT
seawind wrote:
> I am a newer, and now i encounter a simple question.
>
> i want to erase all specified characters ,such as ',' , from a string.
> which function should i call?
> for example:
>
> string ls( "i ,am, a ,dog,");

ls.erase(std::remove(ls.begin(), ls.end(), ','), ls.end())

This also works with other containers. It may seem weird, but it's the
ususal way of doing it in the STL.

You should get yourself a good book about the STL, because a reference
alone isn't enough; you need to know some idioms and concepts.

Jens

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "seawind" <wangdongyu3d@hotmail.com>
Date: Tue, 29 Aug 2006 22:15:14 CST
Raw View
===================================== MODERATOR'S COMMENT:

I'm approving this because it's a response to something that was already
posted, but let's be careful to keep this discussion on-topic. This
discussion is going in a direction that's not directly related to
standardization.

------=_Part_12514_9434602.1156907695370
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

approve<br>comment<br>I'm approving this because it's a response to something that was already posted, but let's be careful to keep this discussion on-topic. This discussion is going in a direction that's not directly related to standardization.
<br>

------=_Part_12514_9434602.1156907695370--


===================================== END OF MODERATOR'S COMMENT

According to what you said,  i try it and it is ok,
thank you for your help and your advice.

and i try another algorithm remove as well. look like this,

string ls(" i , am , dog");

remove(ls.begin(), ls.end(), ',');

of course, i have used namespace std. and i get "i am dogog".
i traced into the function,and found that in fact it calls remove_copy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: jth02@arcor.de (Jens Theisen)
Date: Wed, 30 Aug 2006 16:25:08 GMT
Raw View
seawind wrote:
> of course, i have used namespace std. and i get "i am dogog".
> i traced into the function,and found that in fact it calls remove_copy.

I'll post an answer to comp.lang.c++, as this is off topic here and the
moderators are getting angry I think. :)

Jens

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]