判斷字符串中的字母大小寫、數字的個數
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);
}