Apache StringUtils:专为Java字符串而生的工具类

String)在我们的日常工作中,用得非常非常非常多。

在我们的代码中经常需要对字符串判空,截取字符串、转换大小写、分隔字符串比较字符串、去掉多余空格、拼接字符串、使用正则表达式等等。

如果只用 String 类提供的那些方法,我们需要手写大量的额外代码,不然容易出现各种异常。

现在有个好消息是:org.apache.commons.lang3包下的StringUtils工具类,给我们提供了非常丰富的选择。

Maven 坐标:

复制代码
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

StringUtils 提供了非常多实用的方法,大概有下图的四页到五页,我只截了两页,实在是太多了。

接下来,我们来拿一些常用的方法举例说明。

字符串判空

其实空字符串,不只是 null 一种,还有""," ","null"等等,多种情况。

StringUtils 给我们提供了多个判空的静态方法,例如:

复制代码
String str1 = null;
String str2 = "";
String str3 = " ";
String str4 = "abc";
System.out.println(StringUtils.isEmpty(str1));
System.out.println(StringUtils.isEmpty(str2));
System.out.println(StringUtils.isEmpty(str3));
System.out.println(StringUtils.isEmpty(str4));
System.out.println("=====");
System.out.println(StringUtils.isNotEmpty(str1));
System.out.println(StringUtils.isNotEmpty(str2));
System.out.println(StringUtils.isNotEmpty(str3));
System.out.println(StringUtils.isNotEmpty(str4));
System.out.println("=====");
System.out.println(StringUtils.isBlank(str1));
System.out.println(StringUtils.isBlank(str2));
System.out.println(StringUtils.isBlank(str3));
System.out.println(StringUtils.isBlank(str4));
System.out.println("=====");
System.out.println(StringUtils.isNotBlank(str1));
System.out.println(StringUtils.isNotBlank(str2));
System.out.println(StringUtils.isNotBlank(str3));
System.out.println(StringUtils.isNotBlank(str4));

执行结果:

复制代码
true
true
false
false
=====
false
false
true
true
=====
true
true
true
false
=====
false
false
false
true

示例中的:isEmptyisNotEmptyisBlankisNotBlank,这 4 个判空方法你们可以根据实际情况使用。

优先推荐使用isBlankisNotBlank方法,因为它会把" "也考虑进去。

分隔字符串

分隔字符串是常见需求,如果直接使用 String 类的 split 方法,就可能会出现空指针异常。

复制代码
String str1 = null;
System.out.println(StringUtils.split(str1,","));
System.out.println(str1.split(","));

执行结果:

复制代码
null
Exception in thread "main" java.lang.NullPointerException
\tat com.sue.jump.service.test1.UtilTest.main(UtilTest.java:21)

使用 StringUtils 的 split 方法会返回 null,而使用 String 的 split 方法会报指针异常。

判断是否纯数字

给定一个字符串,判断它是否为纯数字,可以使用isNumeric方法。例如:

复制代码
String str1 = "123";
String str2 = "123q";
String str3 = "0.33";
System.out.println(StringUtils.isNumeric(str1));
System.out.println(StringUtils.isNumeric(str2));
System.out.println(StringUtils.isNumeric(str3));

执行结果:

复制代码
true
false
false

将集合拼接成字符串

有时候,我们需要将某个集合的内容,拼接成一个字符串,然后输出,这时可以使用join方法。例如:

复制代码
List<String> list = Lists.newArrayList("a", "b", "c");
List<Integer> list2 = Lists.newArrayList(1, 2, 3);
System.out.println(StringUtils.join(list, ","));
System.out.println(StringUtils.join(list2, " "));

执行结果:

复制代码
a,b,c
1 2 3

其他方法

这里再列举一些,其他的方法可以自己去研究一下。

  • trim(String str):去除字符串首尾的空白字符。
  • trimToEmpty(String str):去除字符串首尾的空白字符,如果字符串为 null,则返回空字符串。
  • trimToNull(String str):去除字符串首尾的空白字符,如果结果为空字符串,则返回 null。
  • equals(String str1, String str2):比较两个字符串是否相等。
  • equalsIgnoreCase(String str1, String str2):比较两个字符串是否相等,忽略大小写。
  • startsWith(String str, String prefix):检查字符串是否以指定的前缀开头。
  • endsWith(String str, String suffix):检查字符串是否以指定的后缀结尾。
  • contains(String str, CharSequence seq):检查字符串是否包含指定的字符序列。
  • indexOf(String str, CharSequence seq):返回指定字符序列在字符串中首次出现的索引,如果没有找到,则返回 -1。
  • lastIndexOf(String str, CharSequence seq):返回指定字符序列在字符串中最后一次出现的索引,如果没有找到,则返回 -1。
  • substring(String str, int start, int end):截取字符串中指定范围的子串。
  • replace(String str, String searchString, String replacement):替换字符串中所有出现的搜索字符串为指定的替换字符串。
  • replaceAll(String str, String regex, String replacement):使用正则表达式替换字符串中所有匹配的部分。
  • join(Iterable<?> iterable, String separator):使用指定的分隔符将可迭代对象中的元素连接为一个字符串。
  • split(String str, String separator):使用指定的分隔符将字符串分割为一个字符串数组。
  • capitalize(String str):将字符串的第一个字符转换为大写。
  • uncapitalize(String str):将字符串的第一个字符转换为小写。
相关推荐
ChaHae-In10 小时前
SpringBoot统一功能处理
java·spring boot·后端
coder!mq10 小时前
说几个常见的语法糖?
java·开发语言·算法
gugucoding10 小时前
38. 【Java】Stream API(下):高级操作与性能
java·开发语言
心平气和量大福大11 小时前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言
Python大数据分析@11 小时前
使用大模型MCP采集数据,爬虫已经无门槛
python·网络爬虫
9523611 小时前
Spring-cloud配置中心
java·spring·spring cloud·微服务
quantdash_cc11 小时前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
ydd10010011 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
65岁退休Coder12 小时前
LangChain v1.3.4 笔记 - 07 补充:链式调用 LCEL
后端·python·langchain
卷无止境12 小时前
FastAPI 部署在 Nginx 后面到底该怎么配
后端·python