public static void main(String[] args) {
String st = "abA@# 54a";
int ziMu = 0;
int kongGe = 0;
int shuZi = 0;
int qiTa = 0;
for(int i = 0; i < st.length(); i++ ) {
if(st.charAt(i) >= 'A' && st.charAt(i) <= 'z') {
ziMu++;
}else if(st.charAt(i) >= '0' && st.charAt(i) <= '9') {
shuZi++;
}else if(st.charAt(i) == ' ') {
kongGe++;
}else {
qiTa++;
}
}
System.out.println("英文字母个数:" + ziMu);
System.out.println("数字个数:" + shuZi);
System.out.println("空格个数:" + kongGe);
System.out.println("其他字符个数:" + qiTa);
}
这样就可以了 |