java 判断字符串是否含有中文

2024-11-04 23:22:13
推荐回答(2个)
回答1:

Java代码
package com.zakisoft.ch;

public class IsChineseOrNot {

// GENERAL_PUNCTUATION 判断中文码搭的判模厅“号
// CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
// HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号
private static final 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 final boolean isChinese(String strName) {
char[] ch = strName.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c)) {
return true;
}
}
return false;
}

public static void main(String[] args) {
System.out.println(isChinese("き"));
System.out.println(isChinese("test,.?!%^&*(){}[]"));
System.out.println(isChinese("测试"));
System.out.println(isChinese("“测试”,。?!%……&*()——{}【】”"));
}

public static final boolean isChineseCharacter(String chineseStr) {
char[] charArray = chineseStr.toCharArray();
for (int i = 0; i < charArray.length; i++) {
if ((charArray[i] >= 0x4e00) && (charArray[i] <= 0x9fbb)) {
return true;
}
}
return false;
}

/**
* @deprecated; 弃用。和方法isChineseCharacter比效掘隐率太低。
* */
public static final boolean isChineseCharacter_f2() {
String str = "!?";
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + 1).matches("[\\一-\\?]+")) {
return true;
}
}
return false;
}
}

System.out.println(isChinese("き"));
System.out.println(isChinese("test,.?!%^&*(){}[]"));
System.out.println(isChinese("测试"));
System.out.println(isChinese("“测试”,。?!%……&*()——{}【】”"));
运行结果为:
false
false
true
true

回答2:

String regEx = "[\斗谨配u4e00-\u9fa5]"空指;
Pattern p = Pattern.compile(regEx);
if(p.matcher("43443哈哈哈9898").find()){
System.out.println("有汉字"晌明);
}