Removing accent marks (diacritics) from Latin characters for comparison [duplicate]

霸气de小男生 提交于 2019-12-04 22:28:25

问题


I need to compare the names of European places that are written using the Latin alphabet with accent marks (diacritics) on some characters. There are lots of Central and Eastern European names that are written with accent marks like Latin characters on ž and ü, but some people write the names just using the regular Latin characters without accent marks like z and u.

I need a way to have my system recognize for example mšk žilina being the same as msk zilina, and similar for all the other accented characters used. Is there a simple way to do this?


回答1:


You can make use of java.text.Normalizer and a little regex to get rid of the diacritical marks.

public static String removeDiacriticalMarks(String string) {
    return Normalizer.normalize(string, Form.NFD)
        .replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}

Usage example:

String text = "mšk žilina";
String normalized = removeDiacriticalMarks(text);
System.out.println(normalized); // msk zilina


来源:https://stackoverflow.com/questions/3222972/removing-accent-marks-diacritics-from-latin-characters-for-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!