java 实现校验字符串只包含数字
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author yuhuofei
* @version 1.0
* @description
* @date 2022/12/1 11:13
*/
public class NumberUtil {
/**
* 校验一个字符串是否只包含纯数字
*/
public static boolean isDigits(String data) {
String regex = "[0-9]+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(data);
return matcher.matches();
}
public static void main(String[] args) {
String data = "223333444400987654321";
String other = "8w2g9d30^&*%opl2e34";
System.out.println("是否只包含纯数字:" + isDigits(data));
System.out.println("是否只包含纯数字:" + isDigits(other));
}
}