isEmpty 和 isBlank 的用法区别,居然一半的人答不上来.....

admin 2025-02-06 121人围观 ,发现255个评论

也许你两个都不知道,也许你除了isEmpty/isNotEmpty/isNotBlank/isBlank外,并不知道还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank的存在,comeon,让我们一起来探索;这个工具类.

isBank系列

()

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

(null)=true

("")=true

("")=true

(“bob”)=false

("bob")=false


()

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


()

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

(null)=true

(null,“foo”)=true

(null,null)=true

("",“bar”)=true

(“bob”,“”)=true

("bob",null)=true

("",“bar”)=true

(“foo”,“bar”)=false

/***pChecksifanyoneoftheCharSequencesareblank("")ornullandnotwhitespaceonly../p*@paramcsstheCharSequencestocheck,maybenullorempty*@return{@codetrue}ifanyoftheCharSequencesareblankornullorwhitespaceonly*@*/publicstaticbooleanisAnyBlank(finalCharSequencecss){if((css)){returntrue;}for(finalCharSequencecs:css){if(isBlank(cs)){returntrue;}}returnfalse;}
()

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

(null)=false

(null,“foo”)=false

(null,null)=false

("",“bar”)=false

(“bob”,“”)=false

("bob",null)=false

("",“bar”)=false

(“foo”,“bar”)=true

/***pChecksifnoneoftheCharSequencesareblank("")ornullandwhitespaceonly../p*@paramcsstheCharSequencestocheck,maybenullorempty*@return{@codetrue}ifnoneoftheCharSequencesareblankornullorwhitespaceonly*@*/publicstaticbooleanisNoneBlank(finalCharSequencecss){return!isAnyBlank(css);}
StringUtils的其他方法

可以参考官方的文档,里面有详细的描述,有些方法还是很好用的.

方法名

英文解释

中文解释

IsEmpty/IsBlank

checksifaStringcontainstext

检查字符串是否包含文本

Trim/Strip

removesleadingandtrailingwhitespace

删除前导和尾随空格

Equals/Compare

comparestwostringsnull-safe

比较两个字符串是否为null安全的

startsWith

checkifaStringstartswithaprefixnull-safe

检查字符串是否以前缀null安全开头

sWith

checkifaStringswithasuffixnull-safe

检查字符串是否以后缀null安全结尾

IndexOf/LastIndexOf/Contains

null-safeindex-ofchecks

包含空安全索引检查

IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut

index-ofanyofasetofStrings

任意一组字符串的索引

ContainsOnly/ContainsNone/ContainsAny

doesStringcontainsonly/none/anyofthesecharacters

字符串是否仅包含/无/这些字符中的任何一个

Substring/Left/Right/Mid

null-safesubstringextractions

字符串安全提取

SubstringBefore/SubstringAfter/SubstringBetween

substringextractionrelativetootherstrings-相对其他字符串的字符串提取

Split/Join

splitsaStringintoanarrayofsubstringsandviceversa

将字符串拆分为子字符串数组,反之亦然

Remove/Delete

removespartofaString-删除字符串的一部分

Replace/Overlay

SearchesaStringandreplacesoneStringwithanother

搜索字符串,然后用另一个字符串替换

Chomp/Chop

removesthelastpartofaString

删除字符串的最后一部分

AppIfMissing

appsasuffixtotheoftheStringifnotpresent

如果不存在后缀,则在字符串的末尾附加一个后缀

PrepIfMissing

prepsaprefixtothestartoftheStringifnotpresent

如果不存在前缀,则在字符串的开头添加前缀

LeftPad/RightPad/Center/Repeat

padsaString

填充字符串

UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize

changesthecaseofaString

更改字符串的大小写

CountMatches

countsthenumberofoccurrencesofoneStringinanother

计算一个字符串在另一个字符串中出现的次数

IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable

checksthecharactersinaString

检查字符串中的字符

DefaultString

protectsagainstanullinputString

防止输入字符串为空

Rotate

rotate(circularshift)aString

旋转(循环移位)字符串

Reverse/ReverseDelimited

reversesaString-反转字符串

Abbreviate

abbreviatesastringusingellipsisoranothergivenString

使用省略号或另一个给定的String缩写一个字符串

Difference

comparesStringsandreportsontheirdifferences

比较字符串并报告其差异

LevenshteinDistance

thenumberofchangesneededtochangeoneStringintoanother

将一个String转换为另一个String所需的更改次数



猜你喜欢
不容错过