如何将拉丁 unicode 字符替换为 [a-z] 字符 [英] how to replace Latin unicode character to [a-z] characters

查看:75
本文介绍了如何将拉丁 unicode 字符替换为 [a-z] 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将所有拉丁语 unicode 字符转换为它们的 [a-z] 表示

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

在 Java 中还有其他方法吗?例如正则表达式,或工具库

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

用例:

1- 将其他语言的城市名称转换为英语,例如

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

圣埃斯皮里图 --> 圣埃斯皮里托,

Espírito Santo --> Espirito Santo,

推荐答案

此答案需要 Java 1.6 或更高版本,其中添加了 java.text.Normalizer.

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}+", "");

示例:

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);
    }
}

结果:

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

这篇关于如何将拉丁 unicode 字符替换为 [a-z] 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆