zoukankan      html  css  js  c++  java
  • org.apache.commons.lang3.StringUtils类的isNotBlank和isEmpty方法

    今天在做项目的时候遇到一个小bug,org.apache.commons.lang3.StringUtils类的isEmpty不能判断空字符串。

    查询isEmpty方法的源码,可以发现isEmpty不能判断空字符串。

     1 /**
     2      * <p>Checks if a CharSequence is empty ("") or null.</p>
     3      *
     4      * <pre>
     5      * StringUtils.isEmpty(null)      = true
     6      * StringUtils.isEmpty("")        = true
     7      * StringUtils.isEmpty(" ")       = false
     8      * StringUtils.isEmpty("bob")     = false
     9      * StringUtils.isEmpty("  bob  ") = false
    10      * </pre>
    11      *
    12      * <p>NOTE: This method changed in Lang version 2.0.
    13      * It no longer trims the CharSequence.
    14      * That functionality is available in isBlank().</p>
    15      *
    16      * @param cs  the CharSequence to check, may be null
    17      * @return {@code true} if the CharSequence is empty or null
    18      * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
    19      */
    20     public static boolean isEmpty(final CharSequence cs) {
    21         return cs == null || cs.length() == 0;
    22     }

    查询isNotBlank方法的源码,isNotBlank可以判断所有为空的情况。

     1  /**
     2      * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
     3      *
     4      * <pre>
     5      * StringUtils.isBlank(null)      = true
     6      * StringUtils.isBlank("")        = true
     7      * StringUtils.isBlank(" ")       = true
     8      * StringUtils.isBlank("bob")     = false
     9      * StringUtils.isBlank("  bob  ") = false
    10      * </pre>
    11      *
    12      * @param cs  the CharSequence to check, may be null
    13      * @return {@code true} if the CharSequence is null, empty or whitespace
    14      * @since 2.0
    15      * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
    16      */
    17     public static boolean isBlank(final CharSequence cs) {
    18         int strLen;
    19         if (cs == null || (strLen = cs.length()) == 0) {
    20             return true;
    21         }
    22         for (int i = 0; i < strLen; i++) {
    23             if (Character.isWhitespace(cs.charAt(i)) == false) {
    24                 return false;
    25             }
    26         }
    27         return true;
    28     }
    29 
    30     /**
    31      * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
    32      *
    33      * <pre>
    34      * StringUtils.isNotBlank(null)      = false
    35      * StringUtils.isNotBlank("")        = false
    36      * StringUtils.isNotBlank(" ")       = false
    37      * StringUtils.isNotBlank("bob")     = true
    38      * StringUtils.isNotBlank("  bob  ") = true
    39      * </pre>
    40      *
    41      * @param cs  the CharSequence to check, may be null
    42      * @return {@code true} if the CharSequence is
    43      *  not empty and not null and not whitespace
    44      * @since 2.0
    45      * @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
    46      */
    47     public static boolean isNotBlank(final CharSequence cs) {
    48         return !isBlank(cs);
    49     }

    总结:当需要判断字符串为空时最好用org.apache.commons.lang3.isNotBlank方法。

  • 相关阅读:
    未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序 解决方法
    未能从程序集“System.ServiceModel, Version=3.0.0.0问题解决
    HTML5斯诺克桌球俱乐部【译】
    MVC调试时查看生成的sql语句
    小问题 小技巧 :创建虚拟目录并将IIS里面.net配置版本设为2.0
    网页调用服务程序
    WatiN——Web自动化测试(三)【弹出窗口处理】
    WatiN——Web自动化测试(二)
    小问题 小技巧 :网站路径问题
    小问题 小技巧:敲回车默认提交
  • 原文地址:https://www.cnblogs.com/huanlingjisi/p/8795858.html
Copyright © 2011-2022 走看看