Skip to content

Instantly share code, notes, and snippets.

@justpsvm

justpsvm/HanStrCount.java

Last active Jul 13, 2017
Embed
What would you like to do?
[Java 汉字记数] 利用正则表达式完成 #java
public static int hanStrCount(String hanStr){
int count = 0;
for (int i = 0; i < hanStr.length(); i++) {
boolean matches = Pattern.matches("^[\u4E00-\u9FA5]{0,}$",String.valueOf(hanStr.charAt(i)));
if (matches) count++;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.