java判断字符串中是否有数字、字母、汉字
时间: 2023-06-13 11:03:43 浏览: 105
判断字符串中是否有数字:
```java
public static boolean hasDigit(String str) {
if (str == null || str.trim().length() == 0) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
return true;
}
}
return false;
}
```
判断字符串中是否有字母:
```java
public static boolean hasLetter(String str) {
if (str == null || str.trim().length() == 0) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (Character.isLetter(str.charAt(i))) {
return true;
}
}
return false;
}
```
判断字符串中是否有汉字:
```java
public static boolean hasChinese(String str) {
if (str == null || str.trim().length() == 0) {
return false;
}
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= '\u4e00' && c <= '\u9fa5') {
return true;
}
}
return false;
}
```
使用示例:
```java
String str = "abc123中文";
System.out.println(hasDigit(str)); // true
System.out.println(hasLetter(str)); // true
System.out.println(hasChinese(str)); // true
```
阅读全文
相关推荐
![](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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)