how to replace Latin unicode character to [a-z] characters

流过昼夜 提交于 2019-12-23 08:35:29

问题


I'm trying to convert all Latin unicode Character into their [a-z] representations

ó --> o
í --> i

I can easily do one by one for example:

myString = myString.replaceAll("ó","o");

but since there are tons of variations, this approach is just impractical

Is there another way of doing it in Java? for example a regular Expression, or a utility library

USE CASE:

1- city names from another languages into english e.g.

Espírito Santo --> Espirito Santo,


回答1:


This answer requires Java 1.6 or above, which added java.text.Normalizer.

    String normalized = Normalizer.normalize(input, Normalizer.Form.NFD);
    String accentRemoved = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");

Example:

public class Main {
    public static void main(String[] args) {
        String input = "Árvíztűrő tükörfúrógép";
        System.out.println("Input: " + input);
        String normalized = Normalizer.normalize(input, Normalizer.Form.NFD);
        System.out.println("Normalized: " + normalized);
        String accentRemoved = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
        System.out.println("Result: " + accentRemoved);
    }
}

Result:

Input: Árvíztűrő tükörfúrógép
Result: Arvizturo tukorfurogep


来源:https://stackoverflow.com/questions/32717855/how-to-replace-latin-unicode-character-to-a-z-characters

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