我們經常需要在程式中判斷一個字元是否為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