判断字符串有多少个汉字

代码如下:

public static Integer haveChineseNum(String value) {
  int count = 0;
  String regEx = "[\\u4e00-\\u9fa5]";
  String str = value;
  Pattern p = Pattern.compile(regEx);
  Matcher m = p.matcher(str);
  while (m.find()) {
    for (int i = 0; i <= m.groupCount(); i++) {
      count = count + 1;
    }
  }
  System.out.println("值="+value);
  System.out.println("共有 " + count + "个 ");
  return count;
}

  

posted @ 2021-08-04 11:03  信铁寒胜  阅读(224)  评论(0编辑  收藏  举报