java--遍歷字符串


package com.test;

public class zifuchuanbianli {
public static void main(String[] args) {
String s ="ABCDEabcdef123456!@#$%%^";
int big = 0;
int small = 0;
int num = 0;
int other = 0;
for(int i=0;i<s.length();i++) {
//System.out.println(s.charAt(i));
char c = s.charAt(i); //通過索引拿取每一個字符
//判斷是否在此范圍之內
if(c >= 'A' && c <= 'Z') {
big++; //如果滿足大寫,計數器遞增
}else if(c >= 'a' && c <= 'z'){
small++;
}else if(c >= '0' && c <= '9') {
num++;
}else {
other++;
}
}
System.out.println("大寫字母"+big);
System.out.println("小寫字母"+small);
System.out.println("數字"+num);
System.out.println("其他"+other);
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM