Java 判断中文字符

Java判断一个字符串中是否有中文字符有两种方法,但是原理都一样,就是通过Unicode编码来判断,因为中文在Unicode中的编码区间为:0x4e00--0x9fa5

第一种:

  String chineseStr = "中华人民公社abc";
  char[] charArray = chineseStr.toCharArray();
  for(int i=0;i<charArray.length;i++){
    if ((charArray[i] >= 0x4e00)&&(charArray[i] <= 0x9fa5)){
      System.out.println(charArray[i]);
    }		
  }

第二种:

String str = "中国chinese";
for (int i = 0; i < str.length(); i++) {
	System.out.println(str.substring(i, i + 1).matches("[\\u4e00-\\u9fa5]+"));
}

第三种:

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.println(isChinese(c));
				return;
			} else {
				System.out.println(isChinese(c));
				return;
			}
		}
	}

	public static void main(String[] args) {
//		Random r = new Random();
//		for (int i = 0; i < 20; i++)
//			System.out.println(r.nextInt(10) + 1);
		isChinese("き");
		isChinese("中国");
	}
}

 

你可能感兴趣的:(java,String,Random,Class,extension,Forms)