在日常使用中,推荐使用 isBlank 方法,避免了存在空值,程序判非空成立。
StringUtils.isEmpty():当判断的字符串为 "" 或者 null 时,为true,其他的均是false
System.out.println("null: "+StringUtils.isEmpty(null));//true
System.out.println("空: "+StringUtils.isEmpty(""));//true
System.out.println("空格: "+StringUtils.isEmpty(" "));//false
System.out.println("有值1: "+StringUtils.isEmpty(" b "));//false
System.out.println("有值21: "+StringUtils.isEmpty("b"));//false
StringUtils.isNotEmpty():相当于不为空 , = !isEmpty()
StringUtils.isBlank():当要判断的字符串为 "" 或者 " " 或者 null 时,为true,否则为false
System.out.println("null: "+StringUtils.isBlank(null));//true
System.out.println("空: "+StringUtils.isBlank(""));//true
System.out.println("空格: "+StringUtils.isBlank(" "));//true
System.out.println("有值1: "+StringUtils.isBlank(" b "));//false
System.out.println("有值2: "+StringUtils.isBlank("b"));//false
StringUtils.isNotBlank():是否真的不为空,不是空格或者空值 ,相当于!isBlank();
StringUtils.isAnyEmpty():是否有一个为空,只有一个为空,就为true.
System.out.println("多值判空: "+StringUtils.isAnyEmpty(""," ",""));//true
System.out.println("多值判空: "+StringUtils.isAnyEmpty("","",""));//true
System.out.println("多值判空: "+StringUtils.isAnyEmpty("","呵呵",""));//true
System.out.println("多值判空: "+StringUtils.isAnyEmpty("呵呵","呵呵","呵呵"));//false
System.out.println("多值判空: "+StringUtils.isAnyEmpty(" "," "," "));//false
StringUtils.isNoneEmpty():相当于!isAnyEmpty()
, 必须所有的值都不为空才返回true
StringUtils.isAnyBlank():是否包含任何空值(包含 "" 、 " " 、null),一旦有一个非空,都是false
System.out.println("多值判空: "+StringUtils.isAnyBlank(""," ",""));//true
System.out.println("多值判空: "+StringUtils.isAnyBlank("","",""));//true
System.out.println("多值判空: "+StringUtils.isAnyBlank("","呵呵",""));//true
System.out.println("多值判空: "+StringUtils.isAnyBlank(" "," "," "));//true
System.out.println("多值判空: "+StringUtils.isAnyBlank("呵呵","呵呵","呵呵"));//false
StringUtils.isNoneBlank():是否全部都不包含空值: "" 、 " " 、null