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方法。

原文地址:https://www.cnblogs.com/huanlingjisi/p/8795858.html