import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CountTest {
public static void main(String args[]) {
String str = "asdvdfdDERE123ABCD0012587我一二三四五";
System.out.println("Numbers:" + countNumber(str));
System.out.println("Letters:" + countLetter(str));
System.out.println("Chinese:" + countChinese(str));
}
public static int countNumber(String str) {
int count = 0;
Pattern p = Pattern.compile("\\d");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
public static int countLetter(String str) {
int count = 0;
Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
public static int countChinese(String str) {
int count = 0;
Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
}
java统计汉字,字母数字等个数
最新推荐文章于 2024-03-08 11:22:20 发布