在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

isEmpty和isBlank的區別

Android編程精選 ? 來源:博客園 ? 作者:博客園 ? 2022-07-27 10:36 ? 次閱讀


	

開發中經常有些小細節容易忽略,這些小細節往往容易導致代碼缺陷,今天分享一波工具類的小細節

也許你兩個都不知道,也許你除了isEmpty/isNotEmpty/isNotBlank/isBlank外,并不知道還有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank的存在, come on ,讓我們一起來探索org.apache.commons.lang3.StringUtils;這個工具類。

isEmpty系列

StringUtils.isEmpty()

是否為空. 可以看到 " " 空格是會繞過這種空判斷,因為是一個空格,并不是嚴格的空值,會導致isEmpty(" ")=false

StringUtils.isEmpty(null)=true
StringUtils.isEmpty("")=true
StringUtils.isEmpty("")=false
StringUtils.isEmpty("bob")=false
StringUtils.isEmpty("bob")=false
/**
*
*

NOTE:ThismethodchangedinLangversion2.0. *ItnolongertrimstheCharSequence. *ThatfunctionalityisavailableinisBlank().

* *@paramcstheCharSequencetocheck,maybenull *@return{@codetrue}iftheCharSequenceisemptyornull *@since3.0ChangedsignaturefromisEmpty(String)toisEmpty(CharSequence) */ publicstaticbooleanisEmpty(finalCharSequencecs){ returncs==null||cs.length()==0; }

StringUtils.isNotEmpty()

相當于不為空 ,= !isEmpty()

publicstaticbooleanisNotEmpty(finalCharSequencecs){
return!isEmpty(cs);
}

StringUtils.isAnyEmpty()

是否有一個為空,只有一個為空,就為true。

StringUtils.isAnyEmpty(null)=true
StringUtils.isAnyEmpty(null,"foo")=true
StringUtils.isAnyEmpty("","bar")=true
StringUtils.isAnyEmpty("bob","")=true
StringUtils.isAnyEmpty("bob",null)=true
StringUtils.isAnyEmpty("","bar")=false
StringUtils.isAnyEmpty("foo","bar")=false
/**
*@paramcsstheCharSequencestocheck,maybenullorempty
*@return{@codetrue}ifanyoftheCharSequencesareemptyornull
*@since3.2
*/
publicstaticbooleanisAnyEmpty(finalCharSequence...css){
if(ArrayUtils.isEmpty(css)){
returntrue;
}
for(finalCharSequencecs:css){
if(isEmpty(cs)){
returntrue;
}
}
returnfalse;
}

StringUtils.isNoneEmpty()

相當于!isAnyEmpty(css), 必須所有的值都不為空才返回true

/**
*

ChecksifnoneoftheCharSequencesareempty("")ornull.

* *
*StringUtils.isNoneEmpty(null)=false
*StringUtils.isNoneEmpty(null,"foo")=false
*StringUtils.isNoneEmpty("","bar")=false
*StringUtils.isNoneEmpty("bob","")=false
*StringUtils.isNoneEmpty("bob",null)=false
*StringUtils.isNoneEmpty("","bar")=true
*StringUtils.isNoneEmpty("foo","bar")=true
*
* *@paramcsstheCharSequencestocheck,maybenullorempty *@return{@codetrue}ifnoneoftheCharSequencesareemptyornull *@since3.2 */ publicstaticbooleanisNoneEmpty(finalCharSequence...css){

isBank系列

StringUtils.isBlank()

是否為真空值(空格或者空值)

StringUtils.isBlank(null)=true
StringUtils.isBlank("")=true
StringUtils.isBlank("")=true
StringUtils.isBlank("bob")=false
StringUtils.isBlank("bob")=false
/**
*

ChecksifaCharSequenceiswhitespace,empty("")ornull.

*@paramcstheCharSequencetocheck,maybenull *@return{@codetrue}iftheCharSequenceisnull,emptyorwhitespace *@since2.0 *@since3.0ChangedsignaturefromisBlank(String)toisBlank(CharSequence) */ publicstaticbooleanisBlank(finalCharSequencecs){ intstrLen; if(cs==null||(strLen=cs.length())==0){ returntrue; } for(inti=0;iif
(Character.isWhitespace(cs.charAt(i))==false){ returnfalse; } } returntrue; }

StringUtils.isNotBlank()

是否真的不為空,不是空格或者空值 ,相當于!isBlank();

publicstaticbooleanisNotBlank(finalCharSequencecs){
return!isBlank(cs);
}

StringUtils.isAnyBlank()

是否包含任何真空值(包含空格或空值)

StringUtils.isAnyBlank(null)=true
StringUtils.isAnyBlank(null,"foo")=true
StringUtils.isAnyBlank(null,null)=true
StringUtils.isAnyBlank("","bar")=true
StringUtils.isAnyBlank("bob","")=true
StringUtils.isAnyBlank("bob",null)=true
StringUtils.isAnyBlank("","bar")=true
StringUtils.isAnyBlank("foo","bar")=false
/**
*

ChecksifanyoneoftheCharSequencesareblank("")ornullandnotwhitespaceonly..

*@paramcsstheCharSequencestocheck,maybenullorempty *@return{@codetrue}ifanyoftheCharSequencesareblankornullorwhitespaceonly *@since3.2 */ publicstaticbooleanisAnyBlank(finalCharSequence...css){ if(ArrayUtils.isEmpty(css)){ returntrue; } for(finalCharSequencecs:css){ if(isBlank(cs)){ returntrue; } } returnfalse; }

StringUtils.isNoneBlank()

是否全部都不包含空值或空格

StringUtils.isNoneBlank(null)=false
StringUtils.isNoneBlank(null,"foo")=false
StringUtils.isNoneBlank(null,null)=false
StringUtils.isNoneBlank("","bar")=false
StringUtils.isNoneBlank("bob","")=false
StringUtils.isNoneBlank("bob",null)=false
StringUtils.isNoneBlank("","bar")=false
StringUtils.isNoneBlank("foo","bar")=true
/**
*

ChecksifnoneoftheCharSequencesareblank("")ornullandwhitespaceonly..

*@paramcsstheCharSequencestocheck,maybenullorempty *@return{@codetrue}ifnoneoftheCharSequencesareblankornullorwhitespaceonly *@since3.2 */ publicstaticbooleanisNoneBlank(finalCharSequence...css){ return!isAnyBlank(css); }

StringUtils的其他方法

可以參考官方的文檔,里面有詳細的描述,有些方法還是很好用的。

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

方法名 英文解釋 中文解釋
IsEmpty/IsBlank checks if a String contains text 檢查字符串是否包含文本
Trim/Strip removes leading and trailing whitespace 刪除前導和尾隨空格
Equals/Compare compares two strings null-safe 比較兩個字符串是否為null安全的
startsWith check if a String starts with a prefix null-safe 檢查字符串是否以前綴null安全開頭
endsWith check if a String ends with a suffix null-safe 檢查字符串是否以后綴null安全結尾
IndexOf/LastIndexOf/Contains null-safe index-of checks 包含空安全索引檢查
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut index-of any of a set of Strings 任意一組字符串的索引
ContainsOnly/ContainsNone/ContainsAny does String contains only/none/any of these characters 字符串是否僅包含/無/這些字符中的任何一個
Substring/Left/Right/Mid null-safe substring extractions 字符串安全提取
SubstringBefore/SubstringAfter/SubstringBetween substring extraction relative to other strings -相對其他字符串的字符串提取
Split/Join splits a String into an array of substrings and vice versa 將字符串拆分為子字符串數組,反之亦然
Remove/Delete removes part of a String -刪除字符串的一部分
Replace/Overlay Searches a String and replaces one String with another 搜索字符串,然后用另一個字符串替換
Chomp/Chop removes the last part of a String 刪除字符串的最后一部分
AppendIfMissing appends a suffix to the end of the String if not present 如果不存在后綴,則在字符串的末尾附加一個后綴
PrependIfMissing prepends a prefix to the start of the String if not present 如果不存在前綴,則在字符串的開頭添加前綴
LeftPad/RightPad/Center/Repeat pads a String 填充字符串
UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize changes the case of a String 更改字符串的大小寫
CountMatches counts the number of occurrences of one String in another 計算一個字符串在另一個字符串中出現的次數
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable checks the characters in a String 檢查字符串中的字符
DefaultString protects against a null input String 防止輸入字符串為空
Rotate rotate (circular shift) a String 旋轉(循環移位)字符串
Reverse/ReverseDelimited reverses a String -反轉字符串
Abbreviate abbreviates a string using ellipsis or another given String 使用省略號或另一個給定的String縮寫一個字符串
Difference compares Strings and reports on their differences 比較字符串并報告其差異
LevenshteinDistance the number of changes needed to change one String into another 將一個String轉換為另一個String所需的更改次數

審核編輯:湯梓紅


聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 代碼
    +關注

    關注

    30

    文章

    4823

    瀏覽量

    68954
  • 工具
    +關注

    關注

    4

    文章

    314

    瀏覽量

    27888

原文標題:isEmpty 和 isBlank 請別亂用了,小心把服務器搞崩!

文章出處:【微信號:AndroidPush,微信公眾號:Android編程精選】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏

    評論

    相關推薦

    Java 那些最常用的工具類庫

    , StringUtils.isEmpty(""));assertEquals(true, StringUtils.isBlank(null));assertEquals(true
    發表于 06-15 17:18

    stm32與matlab之串口通信

    stm32與matlab之串口通信涉及到的函數serial,fopen,fclose,instrfindall,instrhwinfo,fprintf,fscanf,fwrite,fread,isempty
    發表于 08-17 07:00

    【學習打卡】OpenHarmony工具集之字符串工具·讓代碼更精簡

    值不為空isBlank(str: string) 是否為真空(和isEmpty區別在于空格的判斷)isNotBlank(str: string)不為真空isAnyBlank(...strArr
    發表于 07-26 15:00

    ISP和IAP的區別詳解

    本內容詳細解釋了ISP和IAP的區別,但兩者的操作方式,結果和應用場合有什么區別
    發表于 02-02 15:14 ?1.4w次閱讀

    iphone和itouch的區別_itouch是什么

    iphone和itouch的區別是什么呢?下面就來為大家奉上iPod Touch和iphone的區別對比告訴您iPod Touch和iphone的外觀細節、系統區別是什么,并教您如何辨別iPod Touch/Touch型號及系統
    發表于 09-20 17:04 ?9.9w次閱讀

    工作接地和保護接地的區別

    工作接地和保護接地的區別。工作接地和保護接地的區別
    發表于 12-28 11:49 ?0次下載

    stm32的JTAG和SWD的使用區別

    stm32的JTAG和SWD的使用區別
    發表于 12-17 21:49 ?36次下載

    FLASH和EEPROM的區別

    FLASH和EEPROM的區別
    發表于 03-29 09:09 ?6次下載

    電阻串聯與并聯有什么區別_電阻串聯和并聯的區別

    本文介紹了串聯的定義與串聯主要特點,其次介紹了并聯的定義與并聯主要特點,最后介紹了并聯和串聯的區別與電阻的串聯與并聯區別
    發表于 01-21 11:05 ?14.5w次閱讀
    電阻串聯與并聯有什么<b class='flag-5'>區別</b>_電阻串聯和并聯的<b class='flag-5'>區別</b>

    java學習—null和isEmpty 區別

    本文檔內容介紹了基于java學習null和isEmpty 區別,供參考
    發表于 03-13 10:26 ?0次下載

    風冷和水冷的區別

    風冷和水冷的區別
    的頭像 發表于 06-13 11:43 ?1.9w次閱讀

    linux與android的區別是什么

    linux與android有什么區別?有以下三點區別
    發表于 05-26 09:41 ?2215次閱讀
    linux與android的<b class='flag-5'>區別</b>是什么

    α測試和β測試的區別

    α測試和β測試的區別
    的頭像 發表于 06-29 11:22 ?2.7w次閱讀

    CPU和GPU的區別有哪些

    CPU和GPU的區別有哪些呢?接下來簡單給大家介紹一下關于GPU和CPU的區別。
    的頭像 發表于 01-06 17:07 ?3.1w次閱讀

    MOSFET與IGBT的區別

    MOSFET與IGBT的區別
    的頭像 發表于 11-27 15:36 ?1183次閱讀
    MOSFET與IGBT的<b class='flag-5'>區別</b>
    主站蜘蛛池模板: 午夜影视福利 | 五月天婷婷在线观看 | 国产小视频在线观看免费 | 性猛交毛片 | 天堂网在线资源 | 亚洲婷婷综合网 | 女人张开腿 让男人桶个爽 免费观看 | 国产三级自拍视频 | 午夜神马影院 | 欲色视频 | 久青草国产免费观看 | 午夜美女视频在线观看高清 | 亚洲一区二区三区在线视频 | 久久精品国产亚洲片 | 在线免费亚洲 | 久久不射影院 | 欧美午夜色视频国产精品 | 狠狠色综合久久久久尤物 | 综合一区 | 四虎www成人影院观看 | 黄色网视频 | 国产视频精品久久 | 黄色一及毛片 | 好大好硬好深好爽的视频 | 国产精品成人一区二区三区 | 97人人视频 | 五月网址 | www日本免费| 高清性色生活片久久久 | 国产你懂的视频 | 天天干天天拍天天射 | 很色视频 | 萌白酱一线天粉嫩喷水在线观看 | 一级片免费视频 | 国产三级三级三级 | 日本一卡精品视频免费 | 日本视频免费高清一本18 | 1024手机在线看 | 亚洲人的天堂男人爽爽爽 | 亚洲天堂999 | 亚洲午夜久久久久国产 |