zoukankan      html  css  js  c++  java
  • Android中判断字符是否为中文、韩文、日文

    我们经常需要在程序中判断一个字符是否为CJK(Chinese、Japanese、Korean)语言的字符。

    例如,在Contacts里面程序需要判断联系人姓名的所属语言。

    今天为大家介绍一种NameSplitter中使用的判断字符所属语言的方法。

    以判断字符是否为中文为例。

    首先,通过guessFullNameStyle函数来判断字符所属语言(使用UnicodeBlock来判断);

        public static int guessFullNameStyle(String name) {
            if (name == null) {
                return FullNameStyle.UNDEFINED;
            }
    
            int nameStyle = FullNameStyle.UNDEFINED;
            int length = name.length();
            int offset = 0;
            while (offset < length) {
                int codePoint = Character.codePointAt(name, offset);
                if (Character.isLetter(codePoint)) {
                    UnicodeBlock unicodeBlock = UnicodeBlock.of(codePoint);
    
                    if (!isLatinUnicodeBlock(unicodeBlock)) {
    
                        if (isCJKUnicodeBlock(unicodeBlock)) {
                            // We don't know if this is Chinese, Japanese or Korean -
                            // trying to figure out by looking at other characters in the name
                            return guessCJKNameStyle(name, offset + Character.charCount(codePoint));
                        }
    
                        if (isJapanesePhoneticUnicodeBlock(unicodeBlock)) {
                            return FullNameStyle.JAPANESE;
                        }
    
                        if (isKoreanUnicodeBlock(unicodeBlock)) {
                            return FullNameStyle.KOREAN;
                        }
                    }
                    nameStyle = FullNameStyle.WESTERN;
                }
                offset += Character.charCount(codePoint);
            }
            return nameStyle;
        }
        private static int guessCJKNameStyle(String name, int offset) {
            int length = name.length();
            while (offset < length) {
                int codePoint = Character.codePointAt(name, offset);
                if (Character.isLetter(codePoint)) {
                    UnicodeBlock unicodeBlock = UnicodeBlock.of(codePoint);
                    if (isJapanesePhoneticUnicodeBlock(unicodeBlock)) {
                        return FullNameStyle.JAPANESE;
                    }
                    if (isKoreanUnicodeBlock(unicodeBlock)) {
                        return FullNameStyle.KOREAN;
                    }
                }
                offset += Character.charCount(codePoint);
            }
    
            return FullNameStyle.CJK;
        }

    其次,如果获得的结果是CJK,那么我们还要进一步判断到底是Chinese还是Japanese还是Korean

        /**
         * If the supplied name style is undefined, returns a default based on the
         * language, otherwise returns the supplied name style itself.
         * 
         * @param nameStyle See {@link FullNameStyle}.
         */
        public static int getAdjustedFullNameStyle(int nameStyle) {
            String mLanguage = Locale.getDefault().getLanguage().toLowerCase();
            if (nameStyle == FullNameStyle.UNDEFINED) {
                if (JAPANESE_LANGUAGE.equals(mLanguage)) {
                    return FullNameStyle.JAPANESE;
                } else if (KOREAN_LANGUAGE.equals(mLanguage)) {
                    return FullNameStyle.KOREAN;
                } else if (CHINESE_LANGUAGE.equals(mLanguage)) {
                    return FullNameStyle.CHINESE;
                } else {
                    return FullNameStyle.WESTERN;
                }
            } else if (nameStyle == FullNameStyle.CJK) {
                if (JAPANESE_LANGUAGE.equals(mLanguage)) {
                    return FullNameStyle.JAPANESE;
                } else if (KOREAN_LANGUAGE.equals(mLanguage)) {
                    return FullNameStyle.KOREAN;
                } else {
                    return FullNameStyle.CHINESE;
                }
            }
            return nameStyle;
        }

    恩,大致就是这样。我用Eclipse写了一个小Demo,希望能帮到大家~

    附源码链接:

    http://pan.baidu.com/s/1gdorERh

  • 相关阅读:
    MyBatis:2
    MyBatis:1
    synchronized锁普通方法和锁静态方法
    打印倒直角三角形
    迭代器模拟for循环
    Python迭代对象与迭代器
    ffmpeg用法(心得体会还有你见过的用法)
    ffmpeg命令选项解释
    ffmpeg一些filter用法、以及一些功能命令
    FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
  • 原文地址:https://www.cnblogs.com/Lefter/p/3804051.html
Copyright © 2011-2022 走看看