How to remove accents and other graphic signals from a String in Java? Ex .:
String s = "maçã";
String semAcento = ???; // resultado: "maca"
How to remove accents and other graphic signals from a String in Java? Ex .:
String s = "maçã";
String semAcento = ???; // resultado: "maca"
I usually use regex
along with the Normalizer
class. So:
public static String removerAcentos(String str) {
return Normalizer.normalize(str, Normalizer.Form.NFD).replaceAll("[^\p{ASCII}]", "");
}
If it's Java7 + you can use this solution found in SOen link
First import this:
import java.text.Normalizer;
import java.util.regex.Pattern;
Then add this to your main class or to another class you use:
public static String deAccent(String str) {
String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD);
Pattern pattern = Pattern.compile("\p{InCombiningDiacriticalMarks}+");
return pattern.matcher(nfdNormalizedString).replaceAll("");
}
And at the time of use would look something like:
System.out.print(deAccent("Olá, mundo!"));
It uses the regular expression ( regex ) for change them: \p{InCombiningDiacriticalMarks}+
See working at IDEONE: link