判断字符串是否全为汉字
1 String str1 = "java判断是否为汉字" 2 String str2 = "全为汉字" 3 String reg = "[\u4e00-\u9fa5]+" 4 boolean result1 = str1.matches(reg)//false 5 boolean result2 = str2.matches(reg)//true
提取字符串中的汉字
1 String str = "java怎么把asdasd字符串中的asdasd的汉字取出来"; 2 String reg = "[^u4e00-u9fa5]"; 3 str = str.replaceAll(reg, " "); 4 System.out.println(str);
判断字符串是否含有汉字
1 boolean result = (str.length() == str.getBytes().length)//true:无汉字 false:有汉字
获取字符串中汉字的个数
int count = 0; String reg = "[\u4e00-\u9fa5]"; String str = "java获取汉字Chinese的个数"; Pattern p = Pattern.compile(reg); Matcher m = p.matcher(str); while (m.find()) { for (int i = 0; i <= m.groupCount(); i++) { count = count + 1; } } System.out.println("共有汉字 " + count + "个 ");