您的位置:首页 > 其它

统计字符串中的数字,大写字符,小写字符个数

2018-02-24 15:04 253 查看
public class CharacterTest {
public static void main(String[] args) {
//首先定义一个字符串
String str = "Adu123Messi";
//然后让我们定义三个统计变量
int bigCount = 0;//大写字母
int smallCount = 0;//小写字母
int numberCount = 0;//数字
// 把字符串转换为字符数组。
char[] chs = str.toCharArray();

// 遍历字符数组获取到每一个字符
for (int x = 0; x < chs.length; x++) {
char ch = chs[x];

// 判断该字符
if (Character.isUpperCase(ch)) {
bigCount++;
} else if (Character.isLowerCase(ch)) {
smallCount++;
} else if (Character.isDigit(ch)) {
numberCount++;
}
}
// 输出结果
System.out.println("大写字母:" + bigCount + "个");
System.out.println("小写字母:" + smallCount + "个");
System.out.println("数字字符:" + numberCount + "个");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐