62,614
社区成员
发帖
与我相关
我的任务
分享
public static boolean isChinese(String input){
return input.matches("^[\u4e00-\u9fa5]+$");
}
public static boolean isAllChinese(String str){
for (int i = 0; i < str.length(); i++) { //遍历所有字符
char ch = str.charAt(i);
if(ch < 0x4E00 ||ch > 0x9FA5){ //中文在unicode编码中所在的区间为0x4E00-0x9FA5
return false; //不在这个区间,说明不是中文字符,返回false
}
}
return true; //全部在中文区间,说明全部是中文字符,返回true
}
public static void main(String[] args) {
System.out.println(isAllChinese("你好世界")); //true
System.out.println(isAllChinese("hello世界")); //false
System.out.println(isAllChinese("你好word")); //false
System.out.println(isAllChinese("你好 世界")); //false
}