求字符串空格、数字、字母个数--JAVA基础

来源:互联网 发布:仿没内涵网源码模板 编辑:程序博客网 时间:2024/06/05 09:58

相关内容:charAt()函数

package com.nxl123.www;
public class NumString {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int num[]=new int[4];
// for(int i;i<num.length;i++){
// num[i]=0;
// }
int num[]={0,0,0,0};
String str="I Realily Like You Very Much520";
for(int i=0;i<str.length();i++){
if(str.charAt(i)>='A'&&str.charAt(i)<='Z'){
num[0]++;
}else if (str.charAt(i)>='a'&&str.charAt(i)<='z') {
num[1]++;
}else if (str.charAt(i)>='0'&&str.charAt(i)<='9') {
num[2]++;
}else {
num[3]++;
}
}
System.out.println("大写字母:"+num[0]+"\n"+"字母个数:"+num[1]+"\n"+"数字个数:"+num[2]+"\n"+"空格个数:"+num[3]);
}
}

 示例效果图:

 

阅读全文
0 0