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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM