Android中判斷字元是否為中文、韓文、日文

來源:互聯網
上載者:User

我們經常需要在程式中判斷一個字元是否為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/share/link?shareid=4025849919&uk=2953765628

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.