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;i< strLen; i++) {         if(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

    文章

    4555

    浏览量

    66750
  • 工具
    +关注

    关注

    3

    文章

    305

    浏览量

    27430

原文标题: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.3w次阅读

    iphone4s和iphone4的外观区别_iphone4s外观区别

    iphone4s和iphone4的外观区别,iphone4S和iphone4区别在那里,苹果4S和苹果4区别,苹果4S和苹果4怎么分辨?在这里小编给大家总结了几项iphone4s和iphone4的外观
    发表于 09-18 09:43 1.3w次阅读

    工作接地和保护接地的区别

    工作接地和保护接地的区别。工作接地和保护接地的区别
    发表于 12-28 11:49 0次下载

    NPO与X7R等电容的区别

    NPO与X7R等电容的区别NPO与X7R等电容的区别
    发表于 05-10 16:31 0次下载

    实铜和网铜的区别

    实铜和网铜的区别
    发表于 05-20 11:47 0次下载

    0欧电阻与0欧磁珠的区别

    0欧电阻与0欧磁珠的区别
    发表于 06-19 10:09 0次下载

    java学习—null和isEmpty 区别

    本文档内容介绍了基于java学习null和isEmpty 区别,供参考
    发表于 03-13 10:26 0次下载

    linux与android的区别是什么

    linux与android有什么区别?有以下三点区别
    发表于 05-26 09:41 2015次阅读
    linux与android的<b class='flag-5'>区别</b>是什么

    光交箱和分纤箱的区别

    光交箱和分纤箱两者的区别在于:使用要求不同、特点不同、应用要求不同。
    的头像 发表于 03-08 15:35 2.7w次阅读

    苹果13和12区别大吗

     苹果13和12区别大吗?苹果13和苹果iPhone12有什么区别?下面小编进行简单的说明。
    的头像 发表于 09-23 14:49 1.6w次阅读

    CPU和GPU的区别有哪些

    CPU和GPU的区别有哪些呢?接下来简单给大家介绍一下关于GPU和CPU的区别
    的头像 发表于 01-06 17:07 2.8w次阅读

    光猫直连和光猫拨号有什么区别吗 光猫拨号和不拨号区别

    光猫直连和光猫拨号有什么区别吗 光猫拨号和不拨号区别介绍
    发表于 09-27 17:43 3次下载