I have the following code, which works for me.
public static String quitaDiacriticos(String s) {
s = Normalizer.normalize(s, Normalizer.Form.NFD);
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
return s;
}
this function removes all accents but includes the letter Ññ .. because it returns it as an Nn
I pass him for example 'ÑOñó'
and it returns 'nono'
I need you to return 'ÑOño'
What you want is to eliminate the accents (á, é, í, ó, ú) but keep the tilde or tilde (Ñ, ñ).
You can use a method where you indicate to replace only the unicode characters for the accent that are
\u0300
and\u0301
:For this you can make use of the Normalizer.normalize() method , which transforms Unicode text into an equivalent compound or decompose form, which allows you to sort and search for text more easily, so you can replace it:
now as an example calling the above method:
you will have as output: