首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

字符串结成字符判断

2012-12-27 
字符串组成字符判断/** * 字符串中字符判断 ***/public class StringCheck {public static void main(Stri

字符串组成字符判断
/**
* 字符串中字符判断
*
*
*/
public class StringCheck {

public static void main(String[] args) {

String s = "A BC d90$2*() E";

int digitNum = 0;
int letterNum = 0;
int spaceNum = 0;
int otherNum = 0;

char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];

if (c >= '0' && c <= '9') {
digitNum++;
} else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
letterNum++;
} else if (c == ' ') {
spaceNum++;
} else {
otherNum++;
}
}

System.out.println("字母个数是" + letterNum);
System.out.println("数字个数是" + digitNum);
System.out.println("空格个数是" + spaceNum);
System.out.println("其他字符个数" + otherNum);
}

}

热点排行