这几天工作中遇到判断字符串中是否存在中文的问题,记录下来,以便不忘。
下面是中文判断:
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
String s = "adbdi地老天荒work";
Matcher m = p.matcher(s);
if(m.find()) {
// 如果字符串中含有中文,m.find()判断为true
}
下面是数字判断:
Pattern pattern = Pattern.compile("[0-9]*");
String s = “34.3434哈哈”;
// String s1 = s.replace(".", "").trim();
boolean b = pattern.matcher(s1).matches();
if(!b) {
// 如果字符串中剩下的字符不能匹配数字正则,b为false;
}