實際應用中,經常會用到判斷字符串是否爲空的邏輯
比較簡單的就是用 Str != null && Str.length() >0 來判斷
其實很多java工具集都是有包裝好的接口可以使用的
比如 StringUtils.isEmpty(String str) 和 StringUtils.isBlank(String str)
isEmpty和isBlank的區別在於
isEmpty僅僅是判斷空和長度爲0字符串
isBlank判斷的是空,長度爲0,空白字符(包括空格,製表符\t,換行符\n,換頁符\f,回車\r)組成的字符串。
比如:
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty(“aaa”) = false
StringUtils.isEmpty("\t \n \r \f") = false
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUitls.isBlank(" ") = true
StringUtils.isEmpty(“aaa”) = false
StringUtils.isEmpty("\t \n \r \f") = true