功能需求:判断str是否每一位都是数字
【方法一:ASCII码判断】
数字:
ch - '0' >= 0 && ch - '9' <= 0
大写字母:
ch - 'A' >= 0 && ch - 'Z' <= 0
小写字母:
ch - 'a' >= 0 && ch - 'z' <= 0
【方法二:Character.isDigit()判断数字】
for (int j = 0; j < str.length; j++) {//str有一位不是数字就返回falseif (!Character.isDigit(str[j])) return false;}
【方法三:正则表达式判断】
判断是否是数字: [0-9]*: [0-9]表示只能为0-9范围,*表示出现0次或多次
Pattern pattern = pile("[0-9]*");boolean isMatch = pattern.matcher(str).matches();
判断是否是数字、大小写字母:
[0-9A-Za-z]* 表示范围为0-9或A-Z或a-z,* 表示出现0次或多次
Pattern pattern = pile("[0-9A-Za-z]*");boolean isMatch = pattern.matcher(str).matches();