package wzs.arithmetics;
// 判断字符是否属于中文
public class IsChineseOrEnglish
{
// GENERAL_PUNCTUATION 判断中文的“号
// CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
// HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号
public static boolean isChinese(char c)
{
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)
{
return true;
}
return false;
}
public static void isChinese(String strName)
{
char[] ch = strName.toCharArray();
for (int i = 0; i < ch.length; i++)
{
char c = ch[i];
if (isChinese(c) == true)
{
System.out.print("中文:" + ch[i] + " ");
}
else
{
System.out.print("EN:" + ch[i] + " ");
}
}
}
public static void main(String[] args)
{
isChinese("Ezh美o国gguo");
isChinese("中国F");
}
}
输出结果:
EN:E EN:z EN:h 中文:美 EN:o 中文:国 EN:g EN:g EN:u EN:o 中文:中 中文:国 EN:F