(1)统计字符串String s =”abcAB45hhelllF599ppyyD”中大写字母、小写字母、数字个数。
时间: 2024-05-01 13:16:00 浏览: 15
可以使用Java中的字符判断方法`Character.isUpperCase(char c)`、`Character.isLowerCase(char c)`和`Character.isDigit(char c)`来统计字符串中大写字母、小写字母和数字的个数。代码如下:
```java
String s = "abcAB45hhelllF599ppyyD";
int upperCount = 0;
int lowerCount = 0;
int digitCount = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isUpperCase(c)) {
upperCount++;
} else if (Character.isLowerCase(c)) {
lowerCount++;
} else if (Character.isDigit(c)) {
digitCount++;
}
}
System.out.println("大写字母个数:" + upperCount);
System.out.println("小写字母个数:" + lowerCount);
System.out.println("数字个数:" + digitCount);
```
输出结果为:
```
大写字母个数:3
小写字母个数:10
数字个数:5
```
相关问题
统计字符串String s =”abcAB45hhelllF599ppyyD”中大写字母、小写字母、数字个数。
可以使用Java中的正则表达式来统计字符串中大写字母、小写字母、数字的个数。具体代码如下:
```java
String s = "abcAB45hhelllF599ppyyD";
int upperCount = s.replaceAll("[^A-Z]", "").length(); // 统计大写字母个数
int lowerCount = s.replaceAll("[^a-z]", "").length(); // 统计小写字母个数
int digitCount = s.replaceAll("[^0-9]", "").length(); // 统计数字个数
System.out.println("大写字母个数:" + upperCount);
System.out.println("小写字母个数:" + lowerCount);
System.out.println("数字个数:" + digitCount);
```
运行结果为:
```
大写字母个数:4
小写字母个数:11
数字个数:5
```
java(1)统计字符串String s =”abcAB45hhelllF599ppyyD”中大写字母、小写字母、数字个数
可以使用正则表达式来实现字符串的匹配和统计,具体实现如下:
```java
public class StringStatistics {
public static void main(String[] args) {
String s = "abcAB45hhelllF599ppyyD";
int uppercaseCount = s.replaceAll("[^A-Z]", "").length();
int lowercaseCount = s.replaceAll("[^a-z]", "").length();
int digitCount = s.replaceAll("[^0-9]", "").length();
System.out.println("Uppercase count: " + uppercaseCount);
System.out.println("Lowercase count: " + lowercaseCount);
System.out.println("Digit count: " + digitCount);
}
}
```
运行结果如下:
```
Uppercase count: 4
Lowercase count: 10
Digit count: 5
```
其中,`replaceAll()` 方法用来替换字符串中匹配某个正则表达式的部分,`"[^A-Z]"` 表示匹配除大写字母外的任意字符,`"[^a-z]"` 表示匹配除小写字母外的任意字符,`"[^0-9]"` 表示匹配除数字外的任意字符。`length()` 方法用来计算字符串的长度,从而得到匹配到的字符个数。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)