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

如题所述

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;
    }
}

温馨提示:答案为网友推荐,仅供参考
第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;
        }
    }

}

用正则表达式验证,  取出每个字符,正则表达式比较汉字.

第2个回答  2016-12-03
String s="java判断";
Pattern p=Pattern.compile("[\u4e00-\u9fa5]"); //正则表达式
Matcher m=p.matcher(s);
System.out.print(m.find()); //输出true

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网