How can I in C++ remove all specified characters from the string?
For example If I have a string " &New &York "
and I want to remove the characters from the set {' ','&'}
,the string after removing will look like "NewYork"
.
I prefer not to write a function which iterates a string and checks for each char in string if it is part of the undesired set.
Asked
Active
Viewed 4,557 times
1

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

YAKOVM
- 9,805
- 31
- 116
- 217
-
Possible Duplicate of [this](http://stackoverflow.com/questions/5891610/how-to-remove-characters-from-a-string) – Foggzie Jan 23 '13 at 08:42
-
any function that might exist to do that will just iterate and check each char individually. there's just no other way to do that. why do you want to avoid it? – Andreas Grapentin Jan 23 '13 at 08:42
-
1@AndreasGrapentin I assume the OP meant "I want to use an existing function, not write my own." – Angew is no longer proud of SO Jan 23 '13 at 08:43
3 Answers
4
You can try this:
str.erase(
std::remove_if(str.begin(), str.end(), [](char chr){ return chr == '&' || chr == ' ';}),
str.end());
-
Which requires C++11 (which most people can't yet use). (Of course, it's fairly trivial to write a small functional object to do the same thing.) – James Kanze Jan 23 '13 at 09:17
2
use this
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string s = "the#qu1ck&br0wn ***fox %%jumped 0ver @@the lazy dog";
s.erase( std::remove_if(s.begin(), s.end(), std::isalnum),
s.end() );
}

Babasaheb Gosavi
- 7,735
- 1
- 14
- 14
-
Which has undefined behavior. And won't necessarily even compile. And doesn't do what he requested. – James Kanze Jan 23 '13 at 09:18
1
the easiest, and probably simplest way to do this on a char array (you didn't really specify what your "string" is, so I assume the simplest possible type) would be something like this:
#include <cstring>
void erase(char *str, char *evilchars)
{
char *ptr1 = str;
char *ptr2 = str;
while(*ptr1)
{
*ptr2 = *ptr1;
if(!strchr(evilchars, *ptr1))
++ptr2;
++ptr1;
}
*ptr2 = 0;
}

Andreas Grapentin
- 5,499
- 4
- 39
- 57
-
This is neither easy (a lot of code) nor simple. Using `std::string` and `std::replace_if` is far simpler, and easier to get right too. – James Kanze Jan 23 '13 at 09:19
-
@JamesKanze it's easy to understand, even for a beginner, given they invest a bit of time, and it certainly is a valuable learning experience for pointers. Also, I didn't benchmark it yet, but I guess it may rival the performance of std::replace_if. And I meant "simple" like "uses the least foreign code" – Andreas Grapentin Jan 23 '13 at 09:38
-
In C++, the "simplest possible type" for strings is `std::string`, not `char*`. – Angew is no longer proud of SO Jan 23 '13 at 10:14
-
@AndreasGrapentin There's no foreign code in the `std::replace_if` solution. And there's a lot less code to write and to understand (and maybe get wrong). And I'd be very surprised if calling `strchr` each time in the loop is faster that the `std::replace_if` versions proposed. – James Kanze Jan 23 '13 at 18:35
-
@JamesKanze there should not be much difference, because the argument to strchr is just the array of characters to be removed from the string. But let's not argument about that. I felt that for the sake of completeness, there should be at least one "low level" answer to this question, that tackles it from a kind of different angle. That's why I provided the pointer magic. However, I would be very interested in a little benchmark comparing my approach to std::replace_if. if you had the time to make one, I'd greatly appreciate it :) – Andreas Grapentin Jan 23 '13 at 20:53
-
@AndreasGrapentin I'm not sure I see the value in presenting a (very) low level solution for a relatively high level problem. (If it were a question about how to implement `std::remove_if`, it would be different.) As for benchmarks, any benchmark I could write would only concern the system I have access to. The critical difference hinges on the cost of a function call (to `strchr`), as opposed to the inline evaluation in `remove_if`, and the cost of a function call varies greatly from one system to the other. – James Kanze Jan 24 '13 at 08:47
-
@JamesKanze You're right about the benchmark, given that strchr is never inlined by the compiler, I don't know. But why exactly are you putting so much effort in discrediting a perfectly valid approach to the given problem? – Andreas Grapentin Jan 24 '13 at 10:06
-
@AndreasGrapentin Because it is _not_ a valid approach to the given problem. (And depending on the implementation, `strchr` _could_ be inlined, although I don't know of any compilers which currently do it.) – James Kanze Jan 24 '13 at 10:16
-
@JamesKanze why exactly is it not a valid approach to the given problem? – Andreas Grapentin Jan 24 '13 at 10:23
-
@AndreasGrapentin Because there are much simpler and better solutions. – James Kanze Jan 24 '13 at 11:02
-
@JamesKanze I'll just end this discussion now. I'm sorry my answer does not fit your standards, but You'll have to live with it :) – Andreas Grapentin Jan 24 '13 at 11:38