如题所述
public class test {
public static void main(String[] args) {
System.out.println(isContainsChinese("122地点"));
}
//方法 返回true为包含中文;false不包含
public static boolean isContainsChinese(String str)
{
Pattern pat = Pattern.compile("[\u4e00-\u9fa5]");
Matcher matcher = pat.matcher(str);
boolean flg = false;
if (matcher.find()) {
flg = true;
}
return flg;
}
}
public static void main(String[] args) {
System.out.println(isContainsChinese("122地点"));
}
//方法 返回true为包含中文;false不包含
public static boolean isContainsChinese(String str)
{
Pattern pat = Pattern.compile("[\u4e00-\u9fa5]");
Matcher matcher = pat.matcher(str);
boolean flg = false;
if (matcher.find()) {
flg = true;
}
return flg;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-12-03
public static void main(String args[]) throws Exception{
String s = "123aaa的";
char[] t = s.toCharArray();
for (char a : t) {
// 判断是否为汉字字符
if (Character.toString(a).matches("[\\u4E00-\\u9FA5]+")) {
System.out.print("字符串含有汉字");
break;
}
}
}
String s = "123aaa的";
char[] t = s.toCharArray();
for (char a : t) {
// 判断是否为汉字字符
if (Character.toString(a).matches("[\\u4E00-\\u9FA5]+")) {
System.out.print("字符串含有汉字");
break;
}
}
}
用正则表达式验证, 取出每个字符,正则表达式比较汉字.
第2个回答 2016-12-03
String s="java判断";
Pattern p=Pattern.compile("[\u4e00-\u9fa5]"); //正则表达式
Matcher m=p.matcher(s);
System.out.print(m.find()); //输出true
Pattern p=Pattern.compile("[\u4e00-\u9fa5]"); //正则表达式
Matcher m=p.matcher(s);
System.out.print(m.find()); //输出true