专栏名称: macrozheng
专注Java技术分享,解析优质开源项目。涵盖SpringBoot、SpringCloud、Docker、K8S等实用技术,作者Github开源项目mall(50K+Star)。
目录
相关文章推荐
闹闹每日星运  ·  今生不能错过的队友是TA ·  4 天前  
闹闹每日星运  ·  周运 | 闹闹12星座周运势:0203~0209 ·  4 天前  
闹闹每日星运  ·  相处越久好感度越高的星座 ·  5 天前  
闹闹每日星运  ·  星历0202:巨蟹提高身体素质 ... ·  5 天前  
闹闹每日星运  ·  星历0201:巨蟹拓展好友圈 水瓶适合整理家具 ·  6 天前  
51好读  ›  专栏  ›  macrozheng

工作 3 年的同事不懂 isEmpty 和 isBlank 的区别,真是醉了...

macrozheng  · 公众号  ·  · 2024-09-13 14:10

正文

微服务项目学习: cloud.macrozheng.com

作者:Moshow郑锴
来源:blog.csdn.net/moshowgame/article/details/102914895

新来的同事,干了3年java,代码中 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) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty(“bob”) = false
StringUtils.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 trueif 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;
}

这或许是一个对你有用的开源项目 ,mall项目是一套基于 SpringBoot3 + JDK 17 + Vue 实现的电商系统( Github标星60K ),采用Docker容器化部署,后端支持多模块和微服务架构。包括前台商城项目和后台管理系统,能支持完整的订单流程!涵盖商品、订单、购物车、权限、优惠券、会员、支付等功能!

  • Boot项目: https://github.com/macrozheng/mall
  • Cloud项目: https://github.com/macrozheng/mall-swarm
  • 视频教程: https://www.macrozheng.com/video/

项目演示:

StringUtils.isNotEmpty()

相当于不为空 , = !isEmpty()

public static boolean isNotEmpty(final CharSequence cs) {
        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
/
 * @param css  the CharSequences to check, may be null or empty
 * @return {@code trueif 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 trueif none of the CharSequences are empty or null
 * @since 3.2
 */
public static boolean isNoneEmpty(final CharSequence... css) {

isBank系列

StringUtils.isBlank()

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

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

Checks if a CharSequence is whitespace, empty ("") or null.


 * @param cs  the CharSequence to check, may be null
 * @return {@code trueif 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         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) = true
StringUtils.isAnyBlank(null, “foo”) = true
StringUtils.isAnyBlank(nullnull) = true
StringUtils.isAnyBlank("", “bar”) = true
StringUtils.isAnyBlank(“bob”, “”) = true
StringUtils.isAnyBlank(" bob "null) = true
StringUtils.isAnyBlank(" ", “bar”) = true
StringUtils.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 trueif 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) = false
        StringUtils.isNoneBlank(null, “foo”) = false
        StringUtils.isNoneBlank(nullnull) = false
        StringUtils.isNoneBlank("", “bar”) = false
        StringUtils.isNoneBlank(“bob”, “”) = false
        StringUtils.isNoneBlank(" bob "null) = false
        StringUtils.isNoneBlank(" ", “bar”) = false
        StringUtils.isNoneBlank(“foo”, “bar”) = true
        /
        * 

Checks if none of the CharSequences are blank ("") or null and whitespace only..







请到「今天看啥」查看全文