不多bb,为啥写这个博客,就是加工一些标签的时候 ,常常需要计算金额区间、年龄区间、等等~
本来常用NumberUtils工具包的,有强迫症的我,极其受不了~什么warning,过时的什么鬼~
一怒之下,撸波代码,写个工具类,不多bb,见下面,需要的自取~
package dtwave.junfa.udf.prd.utils;
import java.util.regex.Pattern;
import org.apache.commons.lang3.math.NumberUtils;
/**
* 数字相关工具类
*
* @author zhangchenguang 2019年11月13日
*/
public class NumberUtils_zcg {
/**
* 判断传入数据是否为数字(正则匹配)
*
* @param str
* @return true 表示是数字
* @return false 表示非数字
*
*/
public static boolean isNumber(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
/**
* 判断传入数据是否为数字(直接调用字符串的正则)
*
* @param str
* @return true 表示是数字
* @return false 表示非数字
*
*/
public static boolean isNumber_str(String str) {
if (str.matches("\\d*")) {
return true;
} else {
return false;
}
}
/**
* 根据自带的Character调用相关方法
*
* @param str
* @return
*/
public static boolean isNumber_char(String str) {
for (int i = str.length(); --i >= 0;) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* 根据ASCII码值,来判断是否是数字
*
* @param str
* @return
*/
public static boolean isNumber_ascii(String str) {
for (int i = str.length(); --i >= 0;) {
int chr = str.charAt(i);
if (chr < 48 || chr > 57)
return false;
}
return true;
}
/**
* 通过for循环遍历全部字符串,是否符合数字
*
* @param str
* @return
*/
public static boolean isNumber_for(String str) {
final String number = "0123456789";
for (int i = str.length(); --i >= 0;) {
if (number.indexOf(str.charAt(i)) == -1) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(NumberUtils.isNumber("123"));
}
}