当前位置: 首页 > news >正文

org.apache.commons.lang3包下的StringUtils工具类的使用

 前言

相信平时在写项目的时候,一定使用到StringUtils.isEmpty();StringUtils.isBlank();但是你真的了解他们吗?

也许你两个都不知道,也许你除了isEmpty/isNotEmpty/isNotBlank/isBlank外,并不知道还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank的存在。

 maven对应的包:

        <dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.17.0</version></dependency>

 maven坐标:https://mvnrepository.com/

一、isEmpty()系列

     isEmpty():会绕过空值的判断

    @Testpublic void testStringUtils(){boolean empty = StringUtils.isEmpty(null);System.out.println(empty);//trueboolean empty1 = StringUtils.isEmpty("");System.out.println(empty1);//true//字符串时空,但是输出的是falseboolean empty2 = StringUtils.isEmpty(" ");System.out.println(empty2);//falseboolean empty3 = StringUtils.isEmpty("HelloWorld!");System.out.println(empty3);//falseboolean empty4 = StringUtils.isEmpty(" HelloWorld! ");System.out.println(empty4);//false}

 源码:

    @Deprecatedpublic static boolean isEmpty(@Nullable Object str) {return str == null || "".equals(str);}

 @Deprecated注解:标记该方法过时了,在以后得版本中可能会被代替。

isNotEmpty():相当于不为空

    public static boolean isNotEmpty(final CharSequence cs) {return !isEmpty(cs);}


isAnyEmpty():只要有一个为空,就是true

    @Testpublic void testStringUtils(){System.out.println(StringUtils.isAnyEmpty("Hello", "World"));//falseSystem.out.println(StringUtils.isAnyEmpty(null, "HelloWorld"));//trueSystem.out.println(StringUtils.isAnyEmpty("", " "));//trueSystem.out.println(StringUtils.isAnyEmpty(" ", " "));//false}

源码:

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;
}


isNoneEmpty():相当于!isAnyEmpty(css) , 必须所有的值都不为空才返回true

    @Testpublic void testStringUtils(){System.out.println(StringUtils.isNoneEmpty("Hello", "World"));//trueSystem.out.println(StringUtils.isNoneEmpty(null, "HelloWorld"));//falseSystem.out.println(StringUtils.isNoneEmpty("", " "));//falseSystem.out.println(StringUtils.isNoneEmpty(" ", " "));//true}

二、isBank()系列

和isEmpty()基本一样,但是判断"   "的时候,结果不一样。

     @Testpublic void testStringUtils(){boolean empty2 = StringUtils.isBlank(" ");System.out.println(empty2);//true}

 三、常用的方法

equals():严格的比较两个字符串是不是相等

    @Testpublic void testStringUtils(){System.out.println(StringUtils.equals("HelloWorld", "HelloWorld"));//trueSystem.out.println(StringUtils.equals("", "   "));//falseSystem.out.println(StringUtils.equals("  HelloWorld  ", "HelloWorld"));//falseSystem.out.println(StringUtils.equals("  ", "    "));//falseSystem.out.println(StringUtils.equals("", ""));//trueSystem.out.println(StringUtils.equals(" ", " "));//trueSystem.out.println(StringUtils.equals("",null));//falseSystem.out.println(StringUtils.equals("  ",null));//false}

四、其他的方法

http://www.lryc.cn/news/494858.html

相关文章:

  • HarmonyOS4+NEXT星河版入门与项目实战(23)------组件转场动画
  • 十一、快速入门go语言之接口和反射
  • 智能化图书馆导航系统方案之系统架构与核心功能设计
  • 学习嵩山版《Java 开发手册》:编程规约 - 命名风格(P13 ~ P14)
  • Qt关于padding设置不起作用的的解决办法
  • Golang教程第10篇(语言循环语句-语言循环嵌套)
  • Python Web 开发:FastAPI 入门实战 —— HTTP 基础与 RESTful API 设计
  • uniapp实现组件竖版菜单
  • osg、osgearth源码编译(二)
  • 从单一设备到万物互联:鸿蒙生态崛起的未来之路
  • Kotlin的object修饰符定义类似Java的静态类/静态方法
  • html 中的 <code>标签
  • 【Oracle11g SQL详解】GROUP BY 和 HAVING 子句:分组与过滤
  • SSE基础配置与使用
  • Android -- 简易音乐播放器
  • 【开源免费】基于Vue和SpringBoot的技术交流分享平台(附论文)
  • Python异步编程新写法:asyncio模块的最新实践
  • 【Docker】Docker配置远程访问
  • 网络安全入门之网络安全工具分享-含初期所有工具(附百度网盘链接)
  • 玩转 uni-app 静态资源 static 目录的条件编译
  • Docker 容器隔离关键技术:Seccomp
  • 【大模型】深度解析 NLP 模型5大评估指标及 应用案例:从 BLEU、ROUGE、PPL 到METEOR、BERTScore
  • LinuxC高级
  • 实现PDF文档加密,访问需要密码
  • LangChain——加载知识库文本文档 PDF文档
  • 深度学习2:从零开始掌握PyTorch:数据操作不再是难题
  • MyBatis的if标签的基本使用
  • 【Azure Cache for Redis】Redis的导出页面无法配置Storage SAS时通过az cli来完成
  • 【微服务】Nacos
  • 5、定义与调用函数