栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java从Unicode字符中删除变音符(criticalñṅņṋṉɳȵȵ)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java从Unicode字符中删除变音符(criticalñṅņṋṉɳȵȵ)

我最近在Java中完成了此操作:

public static final Pattern DIACRITICS_AND_FRIENDS    = Pattern.compile("[\p{InCombiningDiacriticalMarks}\p{IsLm}\p{IsSk}]+");private static String stripDiacritics(String str) {    str = Normalizer.normalize(str, Normalizer.Form.NFD);    str = DIACRITICS_AND_FRIENDS.matcher(str).replaceAll("");    return str;}

这将按照你指定的方式进行:

stripDiacritics("Björn")  = Bjorn

但由于诸如Białystok之类的ł人物不讲字,因此它将失败。

如果要使用功能强大的字符串简化程序,则需要进行第二轮清理,以获取一些不是变音符号的特殊字符。是这张地图,我包括了出现在我们客户名称中的最常见的特殊字符。它不是一个完整的列表,但是会为你提供扩展方法的想法。

immutableMap
只是
google-collections
中的一个简单类。

public class StringSimplifier {    public static final char DEFAULT_REPLACE_CHAR = '-';    public static final String DEFAULT_REPLACE = String.valueOf(DEFAULT_REPLACE_CHAR);    private static final ImmutableMap<String, String> NonDIACRITICS = ImmutableMap.<String, String>builder()        //Remove crap strings with no sematics        .put(".", "")        .put(""", "")        .put("'", "")        //Keep relevant characters as seperation        .put(" ", DEFAULT_REPLACE)        .put("]", DEFAULT_REPLACE)        .put("[", DEFAULT_REPLACE)        .put(")", DEFAULT_REPLACE)        .put("(", DEFAULT_REPLACE)        .put("=", DEFAULT_REPLACE)        .put("!", DEFAULT_REPLACE)        .put("/", DEFAULT_REPLACE)        .put("\", DEFAULT_REPLACE)        .put("&", DEFAULT_REPLACE)        .put(",", DEFAULT_REPLACE)        .put("?", DEFAULT_REPLACE)        .put("°", DEFAULT_REPLACE) //Remove ?? is diacritic?        .put("|", DEFAULT_REPLACE)        .put("<", DEFAULT_REPLACE)        .put(">", DEFAULT_REPLACE)        .put(";", DEFAULT_REPLACE)        .put(":", DEFAULT_REPLACE)        .put("_", DEFAULT_REPLACE)        .put("#", DEFAULT_REPLACE)        .put("~", DEFAULT_REPLACE)        .put("+", DEFAULT_REPLACE)        .put("*", DEFAULT_REPLACE)        //Replace non-diacritics as their equivalent characters        .put("u0141", "l") // BiaLystock        .put("u0142", "l") // Bialystock        .put("ß", "ss")        .put("æ", "ae")        .put("ø", "o")        .put("©", "c")        .put("u00D0", "d") // All Ð ð from http://de.wikipedia.org/wiki/%C3%90        .put("u00F0", "d")        .put("u0110", "d")        .put("u0111", "d")        .put("u0189", "d")        .put("u0256", "d")        .put("u00DE", "th") // thorn Þ        .put("u00FE", "th") // thorn þ        .build();    public static String simplifiedString(String orig) {        String str = orig;        if (str == null) { return null;        }        str = stripDiacritics(str);        str = stripNonDiacritics(str);        if (str.length() == 0) { // Ugly special case to work around non-existing empty strings // in Oracle. Store original crapstring as simplified. // It would return an empty string if Oracle could store it. return orig;        }        return str.toLowerCase();    }    private static String stripNonDiacritics(String orig) {        StringBuffer ret = new StringBuffer();        String lastchar = null;        for (int i = 0; i < orig.length(); i++) { String source = orig.substring(i, i + 1); String replace = NONDIACRITICS.get(source); String toReplace = replace == null ? String.valueOf(source) : replace; if (DEFAULT_REPLACE.equals(lastchar) && DEFAULT_REPLACE.equals(toReplace)) {     toReplace = ""; } else {     lastchar = toReplace; } ret.append(toReplace);        }        if (ret.length() > 0 && DEFAULT_REPLACE_CHAR == ret.charAt(ret.length() - 1)) { ret.deleteCharAt(ret.length() - 1);        }        return ret.toString();    }        public static final Pattern DIACRITICS_AND_FRIENDS        = Pattern.compile("[\p{InCombiningDiacriticalMarks}\p{IsLm}\p{IsSk}]+");    private static String stripDiacritics(String str) {        str = Normalizer.normalize(str, Normalizer.Form.NFD);        str = DIACRITICS_AND_FRIENDS.matcher(str).replaceAll("");        return str;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392685.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号