int count = 0;
String regEx = "[\u4e00-\u9fa5]";//[a-zA-z_]
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while(m.find())
{
count ++;
}
本文介绍了一段使用Java正则表达式匹配字符串中所有中文字符的代码片段。通过定义正则表达式并利用Pattern和Matcher类进行匹配,可以统计出给定字符串中中文字符的数量。
int count = 0;
String regEx = "[\u4e00-\u9fa5]";//[a-zA-z_]
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while(m.find())
{
count ++;
}

被折叠的 条评论
为什么被折叠?