zoukankan      html  css  js  c++  java
  • 判空使用isEmpty()方法?这个开发常识你别说自己不知道

    在项目中,我们基本上都会有个StringUtils工具类,用来处理字符串相关的操作,比如:判空,长度,脱敏等。

    今天有个小伙伴,因为调用别人提供的接口,接口里返回参数中有个String类型的。

    小伙伴判空使用的是isEmpty()方法(大多数人认为这个方式没问题)。

    但是问题来了:

    接口返回的String类型参数不是空字符串,是个" "这样的字符串。

    这个isEmpty方法居然返回成了false,那就是没问题了,校验通过了,然后入库了。于是后面的业务逻辑就乱了,接着整条业务线都被坑惨了!他的年终奖也就这么凉凉了~

    其实我们在项目中处理方式一般有两种:

    一种是自定义工具类,这个我们就不扯了。另外一种是使用第三方的工具类,比如:

    commons-lang3中
    org.apache.commons.lang3.StringUtils。

    public static boolean isEmpty(final CharSequence cs) {
    return cs == null || cs.length() == 0;
    }
    public static boolean isBlank(final CharSequence cs) {
    final int strLen = length(cs);
    if (strLen == 0) {
    return true;
    }
    for (int i = 0; i < strLen; i++) {
    if (!Character.isWhitespace(cs.charAt(i))) {
    return false;
    }
    }
    return true;
    }
    spring-core中
    org.springframework.util.StringUtils。

    //判断是否为空
    public static boolean isEmpty(Object str) {
    return (str == null || "".equals(str));
    }
    //判断是否由内容
    public static boolean hasText(String str) {
    return (str != null && str.length() > 0 && containsText(str));
    }
    分析
    上面两个第三方工具类的中isEmpty()方法判断是否为空,但是会有问题:如果其中存在" "这种字符串的时候,空白字符串是会认为不是空(业务代码中基本上都会把空白字符串处理成空,除非有非常特殊情况)。

    isBlank()判断是否为空,处理了" "这种场景。所以如果项目中使用的是org.apache.commons.lang3.StringUtils时,建议使用isBlank()方法。

    hasText()方法是判断是否存在内容,也处理了" "这种字符串,但是需要注意hasText()方法的返回值和isBlank()方法返回值是相反的。

    示例
    使用isEmpty()方法和isBlank()方法:

    import org.apache.commons.lang3.StringUtils;

    public class StringUtilDemo {
    public static void main(String[] args) {
    String name = "";
    String name0 = " ";

        System.out.println(StringUtils.isEmpty(name));
        System.out.println(StringUtils.isEmpty(name0));
    
        System.out.println(StringUtils.isBlank(name));
        System.out.println(StringUtils.isBlank(name0));
    }
    

    }
    输出:

    true
    false
    true
    true
    空白字符串就认为不是空了,但是往往我们在业务代码处理过程中,也需要把空白字符串当做是空来处理,所以在使用的时候回留点心。

    但是isBlank()方法却避免了这种空白字符串的问题。

    使用isEmpty()方法和hasText()方法:

    import org.springframework.util.StringUtils;

    public class StringUtilDemo {
    public static void main(String[] args) {
    String name = "";
    String name0 = " ";

        System.out.println(StringUtils.isEmpty(name));
        System.out.println(StringUtils.isEmpty(name0));
    
        System.out.println(StringUtils.hasText(name));
        System.out.println(StringUtils.hasText(name0));
    }
    

    }
    输出:

    true
    false
    false
    false
    isEmpty()方法也会把空白字符串当做不是空字符串。但是hasText()方法却能避免空白字符串的问题。

    总结
    永远不要指望别人返回来的字符串绝对没有空白字符串。

    不是不推荐使用isEmpty(),而是看你的业务代码里是否把空白字符串当做空字符串来处理。

    如果空白字符串是按照空字符串查来处理,那么推荐使用isBlank()方法或者hasText()方法。

  • 相关阅读:
    centos6.4下django1.11.3项目的部署
    inner join和left join 、right join 的区别?
    php中的对象赋值
    windows下Call to undefined function curl_init() error问题
    include和require的区别误区
    第一车网笔试题
    借贷宝笔试题
    40斤西瓜3人分,求分法
    走楼梯算法
    ip地址分类
  • 原文地址:https://www.cnblogs.com/tianweichang/p/14183467.html
Copyright © 2011-2022 走看看