Java怎么查找字符串大写_Java程序查找字符串中大写,小写,数字和特殊字符的百分比...

将给定的字符串转换为数组中每个字符的字符数组,使用 Character 类的 isUpperCase ()、 isLowerCase ()、 isDigit ()方法验证它是否为大写、小写、数字或任何其他字符。

示例

public class Sample2 {

public static void main(String args[]) {

String data = "Hello HOW are you MR 51";

char [] charArray = data.toCharArray();

int upper = 0;

int lower = 0;

int digit = 0;

int others = 0;

int totalChars = data.length();

for(int i=0; i

if (Character.isUpperCase(charArray[i])) {

upper++;

} else if(Character.isLowerCase(charArray[i])) {

lower++;

} else if(Character.isDigit(charArray[i])){

digit++;

} else {

others++;

}

}

System.out.println("字符串的总长度:"+totalChars);

System.out.println("大写字母:"+upper);

System.out.println("Percentage of upper case letters: "+(upper*100)/totalChars);

System.out.println("小写:"+lower);

System.out.println("小写字母的百分比:"+(lower*100)/totalChars);

System.out.println("Digit :"+digit);

System.out.println("数字百分比:"+(digit*100)/totalChars);

System.out.println("Others :"+others);

System.out.println("其他字符的百分比:"+(others*100)/totalChars);

}

}

输出结果字符串的总长度:23

大写字母:6

Percentage of upper case letters: 26

小写:10

小写字母的百分比:43

Digit :2

数字百分比:8

Others :5

其他字符的百分比:21

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值