1、代码
package com.hainiu.javase;
/**
* 判断字符串的数字个数、空格数、和大小写数
* @author com
*
*/
public class IntegerTest {
public static void main(String[] args) {
String s = " FAD AS 3453s dsfd fsd @f ";
// char[] c = s.toCharArray();
int space = 0,number = 0;
int low = 0,high = 0;
for(int i=0;i=48 && s.charAt(i)<=57) {
++number;
}
//小写字母
else if(s.charAt(i)>=97 && s.charAt(i)<=122) {
++low;
}//大写字母
else if(s.charAt(i)>=65 && s.charAt(i)<=90) {
++high;
}
}
System.out.println("空格个数:"+space);
System.out.println("数字个数:"+number);
System.out.println("大写字母个数:"+low);
System.out.println("小写字母个数:"+high);
System.out.println("字母个数:"+(low+high));
}
}
2、运行结果
3、总结
无需转成char[]数组,for循环字符串,通过String.charAt(i)方法获取字符串的单个字符,然后再通过ASCLL码便可以分别判断字符的数字和大小写字母了。
ASCLL码:十进制0-9:48-57 大写字母A-Z:65-90 小写字母a-z:97-122