Java: remove weird special characters from String

I want to remove all weird special characters from a string in Java. Are these weird special characters appearing on the form ? (question mark) in MS Word. The following is an image of an example string.

enter image description here

+6
source share
3 answers
String string=givenString.replaceAll("[^\\p{ASCII}]", ""); 

\ p {ASCII} are the POSIX character classes. It will replace the non ascii string and return the string (Printable ASCII).

+5
source

This will work:

 String string = yourString.replaceAll("[^\\x00-\\x7F]", ""); 
+1
source

you can use

 String newString = my_string.replaceAll("\\p{C}", ""); 

Learn more about Java Unicode. Java Unicode regex . Regex here

+1
source

All Articles