判断字符串中的字母大小写、数字的个数
public static void main(String[] args) {
String str = "A1bcD1a";
char[] cs = str.toCharArray();
int a = 0; //大写计数
int b = 0; //小写计数
int c = 0; //数字计数
for (int i = 0; i < cs.length; i++) {
if (cs[i]>=65&&cs[i]<=90) {
a++; //大写字母加1
}else if (cs[i]>=97&&cs[i]<=122) {
b++; //小写字母加1
}else {
//无法将char类型直接转化为int类型
//因此先将字符转化为字符串String,再将字符串转化为int类型
String s = String.valueOf(cs[i]);
if (Integer.parseInt(s)>=0&&Integer.parseInt(s)<=9) {
c++; //数字加1
}
}
}
System.out.println("大写字母个数:"+a);
System.out.println("小写字母个数:"+b);
System.out.println("数字个数:"+c);
}