StringUtils.isNotBlank 和 StringUtils.isNotEmpty 以及 (""+null)不为空的探究

来源:互联网 发布:des算法破解 编辑:程序博客网 时间:2024/05/01 00:31

1、StringUtils.isNotBlank()和 StringUtils.isNotEmpty()的区别在于,前者把空格也当做字符串,后者会自动把字符串中的空格忽略,即第一个“ ”不为空为真,后者相反

2、“”是空,null是空,但是“”+null就不是空了,而是  “null”,长度为4的字符串

3、未知变量+“”可以用于int类型转化为字符串类型,但是注意,可能会是null+“”或者int未赋值的时候默认为0+“”,也即虽然原本没有值,但是判断也不为空了。


import org.apache.commons.lang.StringUtils;import org.junit.Test;/** * 判断 null 和 空字符串 * isNotBlank * ""是空 * null是空 * ""+null 不为空 */@Testpublic void test12(){boolean flag=StringUtils.isNotBlank(" ");boolean flag1=StringUtils.isNotBlank("");boolean flag2=StringUtils.isNotBlank(null);boolean flag3=StringUtils.isNotBlank(""+null);System.out.println(flag);//falseSystem.out.println(flag1);//falseSystem.out.println(flag2);//falseSystem.out.println(flag3);//trueSystem.out.println((""+null).length());//null 4 null转化为了字符串}/** * Empty把空格认为是空字符串 */@Testpublic void test13(){boolean flag= StringUtils.isNotEmpty(" ");boolean flag1= StringUtils.isNotEmpty("");boolean flag2=StringUtils.isNotEmpty(null);boolean flag3=StringUtils.isNotEmpty(""+null);System.out.println(flag);//trueSystem.out.println(flag1);//falseSystem.out.println(flag2);//falseSystem.out.println(flag3);//trueSystem.out.println((""+null).length());//null 4 null转化为了字符串}/* * 引申到类型转化 */@Testpublic void test14(){class UserDemo{private int age;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}UserDemo u=new UserDemo();boolean flag1=StringUtils.isNotEmpty(String.valueOf(u.getAge()));boolean flag2=StringUtils.isNotEmpty(Integer.toString(u.getAge()));boolean flag3=StringUtils.isNotEmpty(""+u.getAge());boolean flag4=(Integer.toString(u.getAge())!=null||Integer.toString(u.getAge())!="");boolean flag5=StringUtils.isNotEmpty(u.getName());System.out.println(flag1);//trueSystem.out.println(flag2);//trueSystem.out.println(flag3);//trueSystem.out.println(flag4);//trueSystem.out.println(flag5);//falseSystem.out.println(Integer.toString(u.getAge()).length());//length=1System.out.println(Integer.toString(u.getAge()));//length=1}


0 0
原创粉丝点击