StringUtils(common-lang3)的isBlank方法的判斷大於isEmpty方法

參考博客原址

https://www.cnblogs.com/yaya-yaya/p/6096539.html
https://www.cnblogs.com/zhaoyan001/p/6599477.htmlhtml

StringUtils操做string安全不會報空指針

  • 首先, StringUtils方法的操做對象是java.lang.String類型的對象,是對JDK提供的String類型操做方法的補充,而且是null安全的(即若是輸入參數Stringnull則不會拋出NullPointerException,而是作了相應處理,例如,若是輸入爲null則返回也是null等,具體能夠查看源代碼)。

例子

a.equals(b)

StringUtils.equals(CharSequence c1,CharSequence c2)java

  • 前者若是a爲null,會報null point exception,那用stringutils後者這個封裝好的工具類就不會。

StringUtils的isEmpty和isBlank

  • 前者是要求沒有任何字符,即str==null 或 str.length()==0返回true;web

    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }
  • 後者要求安全

    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    顯而易見包含了isEmpty的條件,且若是string中都是空白字符(無心義字符)也是返回true是blank的svg

    isEmpty判斷的範圍更小,isBlank包含了isEmpty ,例如"    "是blank的
    "工具

    "是blank的(我打了換行)spa

具體用法:

isEmpty

StringUtils.isEmpty(null) = true

StringUtils.isEmpty("") = true

StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格做非空處理 

StringUtils.isEmpty(" ") = false

StringUtils.isEmpty("bob") = false

StringUtils.isEmpty(" bob ") = false

isNotEmpty

 StringUtils.isNotEmpty(null) = false

  StringUtils.isNotEmpty("") = false

  StringUtils.isNotEmpty(" ") = true

  StringUtils.isNotEmpty(" ") = true

  StringUtils.isNotEmpty("bob") = true

  StringUtils.isNotEmpty(" bob ") = true

isBlank

判斷某字符串是否爲空或長度爲0或由空白符(whitespace) 構成指針

 StringUtils.isBlank(null) = true

  StringUtils.isBlank("") = true

  StringUtils.isBlank(" ") = true

  StringUtils.isBlank(" ") = true

  StringUtils.isBlank("\t \n \f \r") = true   //對於製表符、換行符、換頁符和回車符 

  StringUtils.isBlank()   //均識爲空白符 

  StringUtils.isBlank("\b") = false   //"\b"爲單詞邊界符 

  StringUtils.isBlank("bob") = false

  StringUtils.isBlank(" bob ") = false

isNotBlank

斷某字符串是否不爲空且長度不爲0且不禁空白符(whitespace) 構成,等於!isBlank(String str)code

  StringUtils.isNotBlank(null) = false

  StringUtils.isNotBlank("") = false

  StringUtils.isNotBlank(" ") = false

  StringUtils.isNotBlank(" ") = false

  StringUtils.isNotBlank("\t \n \f \r") = false

  StringUtils.isNotBlank("\b") = true

  StringUtils.isNotBlank("bob") = true

  StringUtils.isNotBlank(" bob ") = true