一、StringUtils工具类
是maven项目先添加依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
1.1、字符串为空判断
import org.apache.commons.lang3.StringUtils;
//isNotEmpty =!isEmpty
StringUtils.isEmpty("");//true
StringUtils.isEmpty(" ");//false,注意和isBlank(" ")结果相反
StringUtils.isEmpty(null);//true
StringUtils.isNotEmpty("");//false
StringUtils.isNotEmpty(" ");//true
StringUtils.isNotEmpty(null);//false
//isNotBlank =!isBlank
StringUtils.isBlank("");//true
StringUtils.isBlank(" ");//true,注意和isEmpty(" ")结果相反
StringUtils.isBlank(null);//true
StringUtils.isBlank("\t \n \f \r");//true //对于制表符、换行符、换页符、
StringUtils.isBlank("\b");//false //"\b"为单词边界符
StringUtils.isNotBlank("");//false
StringUtils.isNotBlank(" ");//false
StringUtils.isNotBlank(null);//false
//isAnyBlank和isAnyEmpty是多维判断是否为空
//isNoneBlank = !isAnyBlank;isNoneEmpty = !isAnyEmpty
StringUtils.isAnyBlank("","java","java");//true
StringUtils.isAnyBlank(" ","java","java");//true,注意和isAnyEmpty("
","java","java")结果相反
StringUtils.isAnyEmpty("","java","java");//true
StringUtils.isAnyEmpty(" ","java","java");//false,注意和isAnyBlank("
","java","java")结果相反
//isWhitespace方法用于判断指定字符是否为空白字符,空白符包含:空格、tab键、换行符。
StringUtils.isWhitespace(null);//false
StringUtils.isWhitespace("");//true
StringUtils.isWhitespace(" ");//true
StringUtils.isWhitespace("\t");//true
tringUtils.isWhitespace("\n");//true
总结:
isEmpty判断的条件: str == null || str.length == 0
isBlank判断的条件:str == null || str.length == 0 || str.trim().length == 0isNotEmpty :判断某字符串是否非空
isNotBlank:判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成
二、一些常见字符串问题
1.StringUtils中 isNotEmpty 和isNotBlank的区别:
isNotEmpty(str)等价于 str != null && str.length > 0
isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length > 0//trim()方法的作用是去掉字符串头尾的空格
同理
isEmpty 等价于 str == null || str.length == 0
isBlank 等价于 str == null || str.length == 0 || str.trim().length == 0
str.length > 0 && str.trim().length > 0 ---> str.length > 0
2.String和Date之间的转换
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Nov-2018";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));//重点是这:formatter.format(date)
} catch (ParseException e) {
e.printStackTrace();
}
3.将string 转化成long
long l = Long.parseLong(hahanihao);//hahanihao如果是空,会报空指针错误
4.DecimalFormat数字格式化
DecimalFormat df1 = new DecimalFormat("0.0");
DecimalFormat df2 = new DecimalFormat("#.#");
DecimalFormat df3 = new DecimalFormat("000.000");
DecimalFormat df4 = new DecimalFormat("###.###");
System.out.println(df1.format(12.34)); //结果: 12.3
System.out.println(df2.format(12.34)); //12.3
System.out.println(df3.format(12.34)); // 012.340
System.out.println(df4.format(12.34)); // 12.34
5.Oracle中BigDecimal的int、string相互转换
int或者String转BigDecimal:
String money = 12.34; //字符串型
BigDecimal moneys =new BigDecimal(money); //声明
int moneys1 = moneys.intValue(); //转换int
string moneys2 = moneys.toString(); //转换string
//BigDecimal乘法(BigDecimal类型只能与BigDecimal类型相乘)
BigDecimal prices=new BigDecimal(price).multiply(new BigDecimal(100));
//设置小数位,变量1是小数位数,变量2是取舍方法(四舍五入)
BigDecimal prices1=prices.setScale(2, BigDecimal.ROUND_HALF_UP);
//直接设置某个值的话:
xxx.setXXX(new BigDecimal(xxx));
BigDecimal 转int或者String:
xxx.toString
6.BigDecimal比较选择equals还是compareTo方法
equals方法会比较值和精确度,而compareTo则会忽略精度
public static void main(String[] args) {
BigDecimal z1 = new BigDecimal("0");
BigDecimal z2 = new BigDecimal("0.0");
System.out.println(z1.equals(z2)); //输出结果:false
System.out.println(z1.compareTo(z2));//输出结果:0
}}
//-------------例子2:
public Boolean yearCheck(BigDecimal year, String typeCode) {
List<YearIndexConfig> yicLs = dao.getYearByTypeCode(typeCode);
Boolean result = true;
if(null == yicLs && yicLs.size()<1) {
result = false;
}else {
for (YearIndexConfig yicList : yicLs) {
//yicList.getYear()才是拿到list的BigDecimal 值,才能和BigDecimal 的year比较
if((yicList.getYear().compareTo(year)) == 0) {//等于0,说明这两个值相同
return false;
}
}
}
return result;
}