Reputation: 211
I have this code
public String StripText(String name){
String stripped = name.replaceAll("/:!@#$%^&*()<>+?\"{}[]=`~;", "");
return stripped;
}
which doesn't work. I want it to return a string, erasing occurrences of characters like "/" ":" "!" "@" and so on.
e.g. If I give it a string "puppy:)love", I want it to return a string containing only "puppylove".
Upvotes: 0
Views: 111
Reputation: 174696
You just need to put all the characters inside a character class.With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets.
String stripped = name.replaceAll("[/:!@#$%^&*()<>+?\"{}\\[\\]=`~;]", "");
You also need to escape [
, ]
characters present inside the character class or it would consider the first ]
as an end of charclass.
Example:
String name = "[{puppy:)love}]";
String stripped = name.replaceAll("[/:!@#$%^&*()<>+?\"{}\\[\\]=`~;]", "");
System.out.println(stripped);
Output:
puppylove
Upvotes: 2
Reputation: 36304
You need to escape all the special characters or use []
like this [/:!@#$%^&*()<>+?\"{}[]=
~;]+ in your code
Upvotes: 1