Issue
I have no idea how to remove invalid characters from a string in Java. I'm trying to remove all the characters that are not numbers, letters, or ( ) [ ] . How can I do this?
Thanks
Solution
String foo = "this is a thing with & in it";
foo = foo.replaceAll("[^A-Za-z0-9()\\[\\]]", "");
Javadocs are your friend. Regular expressions are also your friend.
Edit:
That being siad, this is only for the Latin alphabet; you can adjust accordingly. \\w
can be used for a-zA-Z
to denote a "word" character if that works for your case though it includes _
.
Answered By - Brian Roach
Answer Checked By - Willingham (JavaFixing Volunteer)