我们经常需要在程序中判断一个字符是否为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

Android中判断字符是否为中文、韩文、日文的更多相关文章

  1. C#实现判断字符是否为中文

    C#实现判断字符是否为中文 (2012-08-14 14:25:28) 标签: gb2312 big5编码 gbk编码 判断 汉字 杂谈 分类: 技术 protected bool IsChinese ...

  2. Android中判断网络连接是否可用及监控网络状态

    Android中判断网络连接是否可用及监控网络状态 作者: 字体:[增加 减小] 类型:转载 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限,接下来详细介绍Android ...

  3. C#中判断字符是否大写

    在C#中,通常判断一个字符是否为大写字母,有些人可能会第一时间想到用正则表达式,那除了正则表达式,是否还有其他方式呢? 答案是肯定的,先一睹为快,具体代码如下: using System; using ...

  4. HTML基础之JS中的字符转义--转义中文或特殊字符

    1.在标准的url的规范中是不允许出现中文字符或某些特殊字符的,所以要进行转义 2.& 代表参数的链接,如果就是想传& 给后端那么必须转义 decodeURI(url) URl中未转义 ...

  5. (后端)项目中的错误之java中判断字符里面含有某些字符

    数据库的数据出现了数据错误.找到原因是因为代码里面Spring的判断所导致的.其实就是判断字符里有01,走这里,有02,走那里,全是if,但是是类似indexOf的那种判断,偏偏有一个数据是0102, ...

  6. SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理

    SpringBoot中配置字符编码一共有两种方式 方式一: 使用传统的Spring提供的字符编码过滤器(和第二种比较,此方式复杂,由于时间原因这里先不介绍了,后续补上) 方式二(推荐使用) 在appl ...

  7. JAVA中判断char是否是中文的几种方法

    1.方法一 char c = 'a'; if((c >= 0x4e00)&&(c <= 0x9fbb)) { System.out.println("是中文&qu ...

  8. PHP中判断字符串是否含有中文

    <?php /** * [1.测试一] * 当$str = '中文测试'; 时输出"全部是汉字";当$str = '中a文3测试'; 时输出"不全是汉字" ...

  9. Android中判断当前网络是否可用

    转载原文地址:http://www.cnblogs.com/renqingping/archive/2012/10/18/Net.html 当前有可用网络,如下图: 当前没有可用网络,如下图: 实现步 ...

随机推荐

  1. perl入门知识(3)

    引用       在很多场合下使用引用传值,能在很大程度上提高代码的运行效率.       定义一个引用在变量名前加”\”就可以了,如:       $ra=\$a;       $rb=\@b;   ...

  2. Java自学入门新的体会0.2

    Java 基本数据类型 变量就是申请内存来存储值,也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来存储该类型数据. 因此,通过定义不 ...

  3. BZOJ 2467: [中山市选2010]生成树(矩阵树定理+取模高斯消元)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2467 题意: 思路:要用矩阵树定理不难,但是这里的话需要取模,所以是需要计算逆元的,但是用辗转相减会 ...

  4. 为什么mongo中不能用int作为key

    为什么mongo中不能用int作为key??

  5. python 压缩tar 包

    import tarfile import os def make_targz(output_filename, source_dir): print("doing!") with ...

  6. CentOS Gnome字体不清晰

    需要安装字体:dejavu-sans-mono-fonts The package you need to install is dejavu-sans-mono-fonts. This is the ...

  7. Codeforces 101487E - Enter The Dragon

    101487E - Enter The Dragon 思路:做的时候两个地方理解错了,第一个事我以为龙吸了水,水就干了,其实龙是在下雨之前吸的,下雨时湖水又满了,所以湖水永远不会干:第二个是以为只要找 ...

  8. [可能没有默认的字体]Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename...

    Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename... [可能没有默认的字体] 例: //putenv('G ...

  9. Python 运算符重载

    https://www.cnblogs.com/hotbaby/p/4913363.html

  10. win10 自己DIY的arp绑定小脚本

    @echo off&mode con cols=80 lines=22&title ARP_bind Tools setlocal enabledelayedexpansion rem ...