java判斷一個字符串是否為空,isEmpty和isBlank的區別


     轉載於:https://blog.csdn.net/liusa825983081/article/details/78246792

實際應用中,經常會用到判斷字符串是否為空的邏輯

比較簡單的就是用 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


免責聲明!

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



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