StringUtils.isEmpty

StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 null 则不会抛出 NullPointerException ,而是做了相应处理,例如,如果输入为 null 则返回也是 null 等,具体可以查看源代码)。

除了构造器,StringUtils 中一共有130多个方法,并且都是 static 的,所以我们可以这样调用 StringUtils.xxx()

public static boolean isEmpty(String str)

 说明:判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0 
 返回:true/false

注意:isEmpty代表的是空串(“”)和null值,不包含空白符;

示例:

    // isEmpty判断
      System.out.println(StringUtils.isEmpty("")); // true
      System.out.println(StringUtils.isEmpty(" ")); // false
      System.out.println(StringUtils.isEmpty("     ")); // false
      System.out.println(StringUtils.isEmpty("\t")); // false
      System.out.println(StringUtils.isEmpty("\r")); // false
      System.out.println(StringUtils.isEmpty("\n")); // false
      System.out.println( StringUtils.isEmpty(null)); // true
      System.out.println();

注意: 在 StringUtils 中空格作非空处理

import org.apache.commons.lang3.StringUtils;

public class TestIsEmpty {

    public static void main(String[] args) {
        String str = "abc";
        System.out.println(StringUtils.isEmpty(str));//false
    }
}

运行结果:false

猜你喜欢

转载自blog.csdn.net/expect521/article/details/81103342