衡阳派盒市场营销有限公司

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

isEmpty和isBlank的區(qū)別

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


	

開發(fā)中經(jīng)常有些小細(xì)節(jié)容易忽略,這些小細(xì)節(jié)往往容易導(dǎo)致代碼缺陷,今天分享一波工具類的小細(xì)節(jié)

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

isEmpty系列

StringUtils.isEmpty()

是否為空. 可以看到 " " 空格是會(huì)繞過這種空判斷,因?yàn)槭且粋€(gè)空格,并不是嚴(yán)格的空值,會(huì)導(dǎo)致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()

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

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

StringUtils.isAnyEmpty()

是否有一個(gè)為空,只有一個(gè)為空,就為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()

相當(dāng)于!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()

是否真的不為空,不是空格或者空值 ,相當(dāng)于!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的其他方法

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

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 刪除前導(dǎo)和尾隨空格
Equals/Compare compares two strings null-safe 比較兩個(gè)字符串是否為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安全結(jié)尾
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 字符串是否僅包含/無/這些字符中的任何一個(gè)
Substring/Left/Right/Mid null-safe substring extractions 字符串安全提取
SubstringBefore/SubstringAfter/SubstringBetween substring extraction relative to other strings -相對(duì)其他字符串的字符串提取
Split/Join splits a String into an array of substrings and vice versa 將字符串拆分為子字符串?dāng)?shù)組,反之亦然
Remove/Delete removes part of a String -刪除字符串的一部分
Replace/Overlay Searches a String and replaces one String with another 搜索字符串,然后用另一個(gè)字符串替換
Chomp/Chop removes the last part of a String 刪除字符串的最后一部分
AppendIfMissing appends a suffix to the end of the String if not present 如果不存在后綴,則在字符串的末尾附加一個(gè)后綴
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 計(jì)算一個(gè)字符串在另一個(gè)字符串中出現(xiàn)的次數(shù)
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable checks the characters in a String 檢查字符串中的字符
DefaultString protects against a null input String 防止輸入字符串為空
Rotate rotate (circular shift) a String 旋轉(zhuǎn)(循環(huán)移位)字符串
Reverse/ReverseDelimited reverses a String -反轉(zhuǎn)字符串
Abbreviate abbreviates a string using ellipsis or another given String 使用省略號(hào)或另一個(gè)給定的String縮寫一個(gè)字符串
Difference compares Strings and reports on their differences 比較字符串并報(bào)告其差異
LevenshteinDistance the number of changes needed to change one String into another 將一個(gè)String轉(zhuǎn)換為另一個(gè)String所需的更改次數(shù)

審核編輯:湯梓紅


聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 代碼
    +關(guān)注

    關(guān)注

    30

    文章

    4828

    瀏覽量

    69058
  • 工具
    +關(guān)注

    關(guān)注

    4

    文章

    314

    瀏覽量

    27912

原文標(biāo)題:isEmpty 和 isBlank 請(qǐng)別亂用了,小心把服務(wù)器搞崩!

文章出處:【微信號(hào):AndroidPush,微信公眾號(hào):Android編程精選】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    Java 那些最常用的工具類庫

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

    stm32與matlab之串口通信

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

    【學(xué)習(xí)打卡】OpenHarmony工具集之字符串工具·讓代碼更精簡

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

    ISP和IAP的區(qū)別詳解

    本內(nèi)容詳細(xì)解釋了ISP和IAP的區(qū)別,但兩者的操作方式,結(jié)果和應(yīng)用場合有什么區(qū)別
    發(fā)表于 02-02 15:14 ?1.4w次閱讀

    iphone和itouch的區(qū)別_itouch是什么

    iphone和itouch的區(qū)別是什么呢?下面就來為大家奉上iPod Touch和iphone的區(qū)別對(duì)比告訴您iPod Touch和iphone的外觀細(xì)節(jié)、系統(tǒng)區(qū)別是什么,并教您如何辨別iPod Touch/Touch型號(hào)及系統(tǒng)
    發(fā)表于 09-20 17:04 ?9.9w次閱讀

    工作接地和保護(hù)接地的區(qū)別

    工作接地和保護(hù)接地的區(qū)別。工作接地和保護(hù)接地的區(qū)別
    發(fā)表于 12-28 11:49 ?0次下載

    stm32的JTAG和SWD的使用區(qū)別

    stm32的JTAG和SWD的使用區(qū)別
    發(fā)表于 12-17 21:49 ?36次下載

    FPGA與ARM、DSP的區(qū)別

    FPGA與ARM、DSP的區(qū)別
    發(fā)表于 03-15 08:00 ?9次下載

    電阻串聯(lián)與并聯(lián)有什么區(qū)別_電阻串聯(lián)和并聯(lián)的區(qū)別

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

    java學(xué)習(xí)—null和isEmpty 區(qū)別

    本文檔內(nèi)容介紹了基于java學(xué)習(xí)null和isEmpty 區(qū)別,供參考
    發(fā)表于 03-13 10:26 ?0次下載

    linux與android的區(qū)別是什么

    linux與android有什么區(qū)別?有以下三點(diǎn)區(qū)別
    發(fā)表于 05-26 09:41 ?2220次閱讀
    linux與android的<b class='flag-5'>區(qū)別</b>是什么

    α測試和β測試的區(qū)別

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

    windows與macos發(fā)展與區(qū)別精品

    windows與macos發(fā)展與區(qū)別精品(通信電源技術(shù)2019第七期)-windows與macos發(fā)展與區(qū)別精品? ? ? ? ? ? ? ??
    發(fā)表于 09-24 10:15 ?3次下載
    windows與macos發(fā)展與<b class='flag-5'>區(qū)別</b>精品

    CPU和GPU的區(qū)別有哪些

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

    MOSFET與IGBT的區(qū)別

    MOSFET與IGBT的區(qū)別
    的頭像 發(fā)表于 11-27 15:36 ?1198次閱讀
    MOSFET與IGBT的<b class='flag-5'>區(qū)別</b>
    保时捷百家乐官网娱乐城| 百家乐投注网站是多少| 力博娱乐| 网上百家乐软件大全酷| 玩百家乐官网有几种公式| 平利县| 全讯网备用| 玩百家乐官网都是什么人| 百家乐官网游戏程序下载| 澳门博彩娱乐有限公司| 狮威百家乐娱乐| 百家乐赌牌技巧| 百家乐官网高人玩法| 大发888娱乐场下载 注册| 百家乐打水论坛| 百家乐打水策略| 金域百家乐官网的玩法技巧和规则| 冠军百家乐官网现金网| 88娱乐城址| 大发888怎么找| 网上百家乐记牌软件| 百家乐游戏机在哪有| 网址百家乐官网的玩法技巧和规则| 好运来百家乐官网现金网| 白金国际| 大发888客户端官方下载| 金木棉百家乐的玩法技巧和规则| 百家乐天下第一缆| 模拟百家乐官网的玩法技巧和规则| 百家乐官网庄闲最佳打法| 法库县| 篮球比分直播| 大发888游戏平台 新葡京| 神州百家乐的玩法技巧和规则| 稳赢的百家乐投注方法| 首席百家乐官网的玩法技巧和规则| 太原百家乐官网招聘| 百家乐官网二十一点| 百家乐官网视频游365| 密云县| 百家乐官网赢钱战略|