isEmpty和isBlank的区别

开发中经常有些小细节容易忽略,这些小细节往往容易导致代码缺陷,今天分享一波工具类的小细节
也许你两个都不知道,也许你除了isempty/isnotempty/isnotblank/isblank外,并不知道还有isanyempty/isnoneempty/isanyblank/isnoneblank的存在, come on ,让我们一起来探索org.apache.commons.lang3.stringutils;这个工具类。
  isempty系列 stringutils.isempty() 是否为空. 可以看到 空格是会绕过这种空判断,因为是一个空格,并不是严格的空值,会导致 isempty( )=false
stringutils.isempty(null) = truestringutils.isempty() = truestringutils.isempty( ) = falsestringutils.isempty(bob) = falsestringutils.isempty( bob ) = false/** * * note: this method changed in lang version 2.0. * it no longer trims the charsequence. * that functionality is available in isblank().
 * * @param cs  the charsequence to check, may be null * @return {@code true} if the charsequence is empty or null * @since 3.0 changed signature from isempty(string) to isempty(charsequence) */public static boolean isempty(final charsequence cs) {    return cs == null || cs.length() == 0;}     stringutils.isnotempty() 相当于不为空 , = !isempty()。
public static boolean isnotempty(final charsequence cs) {        return !isempty(cs);    }     stringutils.isanyempty() 是否有一个为空,只有一个为空,就为true。
stringutils.isanyempty(null) = truestringutils.isanyempty(null, foo) = truestringutils.isanyempty(, bar) = truestringutils.isanyempty(bob, ) = truestringutils.isanyempty( bob , null) = truestringutils.isanyempty( , bar) = falsestringutils.isanyempty(foo, bar) = false/** * @param css  the charsequences to check, may be null or empty * @return {@code true} if any of the charsequences are empty or null * @since 3.2 */public static boolean isanyempty(final charsequence... css) {  if (arrayutils.isempty(css)) {    return true;  }  for (final charsequence cs : css){    if (isempty(cs)) {      return true;    }  }  return false;}     stringutils.isnoneempty() 相当于!isanyempty(css) , 必须所有的值都不为空才返回true
/** * checks if none of the charsequences are empty () or null.
 * *  * 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 *  * * @param css  the charsequences to check, may be null or empty * @return {@code true} if none of the charsequences are empty or null * @since 3.2 */public static boolean isnoneempty(final charsequence... css) {   isbank系列     stringutils.isblank() 是否为真空值(空格或者空值)
stringutils.isblank(null) = truestringutils.isblank() = truestringutils.isblank( ) = truestringutils.isblank(bob) = falsestringutils.isblank( bob ) = false/** * checks if a charsequence is whitespace, empty () or null.
 * @param cs  the charsequence to check, may be null * @return {@code true} if the charsequence is null, empty or whitespace * @since 2.0 * @since 3.0 changed signature from isblank(string) to isblank(charsequence) */public static boolean isblank(final charsequence cs) {    int strlen;    if (cs == null || (strlen = cs.length()) == 0) {        return true;    }    for (int i = 0; i < strlen; i++) {        if (character.iswhitespace(cs.charat(i)) == false) {            return false;        }    }    return true;}     stringutils.isnotblank() 是否真的不为空,不是空格或者空值 ,相当于!isblank();
public static boolean isnotblank(final charsequence cs) {        return !isblank(cs);    }     stringutils.isanyblank() 是否包含任何真空值(包含空格或空值)
stringutils.isanyblank(null) = truestringutils.isanyblank(null, foo) = truestringutils.isanyblank(null, null) = truestringutils.isanyblank(, bar) = truestringutils.isanyblank(bob, ) = truestringutils.isanyblank( bob , null) = truestringutils.isanyblank( , bar) = truestringutils.isanyblank(foo, bar) = false /** * checks if any one of the charsequences are blank () or null and not whitespace only..
 * @param css  the charsequences to check, may be null or empty * @return {@code true} if any of the charsequences are blank or null or whitespace only * @since 3.2 */public static boolean isanyblank(final charsequence... css) {  if (arrayutils.isempty(css)) {    return true;  }  for (final charsequence cs : css){    if (isblank(cs)) {      return true;    }  }  return false;}     stringutils.isnoneblank() 是否全部都不包含空值或空格
stringutils.isnoneblank(null) = falsestringutils.isnoneblank(null, foo) = falsestringutils.isnoneblank(null, null) = falsestringutils.isnoneblank(, bar) = falsestringutils.isnoneblank(bob, ) = falsestringutils.isnoneblank( bob , null) = falsestringutils.isnoneblank( , bar) = falsestringutils.isnoneblank(foo, bar) = true/** * checks if none of the charsequences are blank () or null and whitespace only..
 * @param css  the charsequences to check, may be null or empty * @return {@code true} if none of the charsequences are blank or null or whitespace only * @since 3.2 */public static boolean isnoneblank(final charsequence... 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所需的更改次数


条码打印机的打印方式
鹅贝贝牧场理财拆分复利游戏开发
保姆级的OpenHarmony创新赛赋能直播课程来了!
警醒华为!Windows Phone失败的四点原因
OPPO首款支持连续血氧监测手环黑金版全渠道开售
isEmpty和isBlank的区别
SMBus I/O扩展器控制LCD偏置电压
步进电机的矩角特性是什么
联发科技将持续携手全球通信行业伙伴共同推动5G与后5G技术的演进
阻尼片是什么材质的_阻尼片的危害
工业环境中的Raspberry PI和Arduino
华为Mate30Pro拍摄样张公布 双4000万像素徕卡电影四摄的魅力有多大
艾默生网络能源推出型号为NPT43-M和NPT44-M的直流电源
芯片国产化热潮来袭,但火热市场背后暗藏问题
串口MRAM芯片MR25H10CDF概述及特点
小米发布两款新品 米粉的狂欢
美联储计划发行与网站域名一致的数字货币
3G手机狂掀补贴新政 北京电信实施3G双重补贴
浅析zynq的优势
员工将与机器人相辅相成 合力发展制造及仓储业