欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > org.apache.commons.lang3包下的StringUtils工具类的使用

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

2025/3/14 7:22:10 来源:https://blog.csdn.net/liusaidh/article/details/144161237  浏览:    关键词: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}

四、其他的方法

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词