user3187166 user3187166 - 1 year ago 103
Android Question

remove all special character from string java

I wanted to know how I can do to completely eliminate ALL of the special characters from a string. In other words I would leave only the words, thus eliminating any other characters as +-òç@èé etc.

Now i use

myString = Normalizer.normalize(myString, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");

But some characters speacials still remain.

Answer Source

Replace the \p{ASCII} regex class with a stricter set that only contains the chars you allow. For example,

myString =  Normalizer.normalize(myString, Normalizer.Form.NFD).replaceAll("[^a-zA-Z]", "");

will first decompose accented chars like é to two parts e + combining ´ (normal form D) and then the regex will remove any character that is not ASCII a..z or A..Z.