public class Main {
public static void main(String[] args) {
String str1 = "12345";
String str2 = "123a45";
System.out.println("Is '" + str1 + "' numeric? " + isNumeric(str1)); // true
System.out.println("Is '" + str2 + "' numeric? " + isNumeric(str2)); // false
}
// 判断字符串是否为纯数字的方法
public static boolean isNumeric(String str) {
// 如果字符串为空或长度为0,返回false
if (str == null || str.length() == 0) {
return false;
}
// 使用正则表达式判断字符串是否为纯数字
return str.matches("\\d+");
}
}
isNumeric
方法用于判断一个字符串是否为纯数字。\\d+
来匹配字符串,其中 \\d
表示任意数字字符,+
表示前面的字符(即数字)可以出现一次或多次。false
。main
方法中,通过两个示例字符串 str1
和 str2
来演示如何调用 isNumeric
方法并输出结果。上一篇:java实现aes的五种加密模式
下一篇:java copy对象
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站