# 八 常见数据封装工具
该工具的主要目的是对常见的基础数据类型的数据进行处理,实现数值大小比较和字符串处理等功能。
该工具包的功能如下:
工具名称 | 功能说明 |
---|---|
BetweenUtil (opens new window) | 比较工具工具类 |
HumpUtil (opens new window) | 下划线与驼峰互转工具 |
NumberUtil (opens new window) | 数字转换与操作比较工具 |
ObjUtil (opens new window) | 集合对象判断工具 |
RegexUtil (opens new window) | 正则工具 |
StringUtil (opens new window) | 字符串工具 |
# 8.1 下划线与驼峰互转工具
该工具主要目的是实现下划线与驼峰互相转换。
工具路径:
com.yishuifengxiao.common.tool.lang.HumpUtil
示例代码:
//转换为驼峰,例如:将call_back转换成 CallBack
String camelCaseName = HumpUtil.camelCaseName("call_back");
System.out.println(camelCaseName);
//转换为下划线,例如:将CallBack转换成 call_back
String underscoreName = HumpUtil.underscoreName("camelCaseName");
System.out.println(underscoreName);
# 8.2 数字转换与操作比较工具
该工具的主要目的是实现字符串与数字之间互相转换以及进行数据大小比较
工具路径
com.yishuifengxiao.common.tool.lang.NumberUtil
示例代码:
//将字符串转换成数字
Integer integer = NumberUtil.parseInt("11");
//判断数字是否小于0
boolean lessZero = NumberUtil.lessZero(integer);
//判断数字是否小于或等于0
boolean lessEqualZero = NumberUtil.lessEqualZero(integer);
//判断数字是否大于0
boolean greaterZero = NumberUtil.greaterZero(integer);
//判断数字是否大于或等于0
boolean greaterEqualZero = NumberUtil.greaterEqualZero(integer);
//判断两个数字是否相等
boolean equals = NumberUtil.equals(Integer.parseInt("2322"), Integer.parseInt("5865"));
//将数字转成Boolean值,如果数字为null,返回为false,数字小于或等于0返回为false,数字大于0返回为true
Boolean num2Bool = NumberUtil.num2Bool(1);
//将boolean值转换成数字,value为true时返回1,否则为0
int bool2Int = NumberUtil.bool2Int(true);