Apache Commons Lang 是一个常用的 Java 库,提供了一系列字符串处理和其他常见任务的实用工具类。其中包括 StringUtils 类,它提供了许多字符串操作的方法,以简化开发者在处理字符串时的工作。
引入依赖
Maven
<properties>
<common.lang.version>3.8</common.lang.version>
</properties>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${common.lang.version}</version>
</dependency>
库简介
org.apache.commons.lang3 #提供高度可重用的静态实用程序方法,主要用于为java.lang类增加值。
org.apache.commons.lang3.arch #提供类以使用os.arch系统属性的值。
org.apache.commons.lang3.builder #帮助创建一致的equals(Object)、toString()、hashCode()和compareTo(Object)方法。
org.apache.commons.lang3.compare #提供类以使用可比较接口和比较接口。
org.apache.commons.lang3.concurrent #为多线程编程提供支持类。
org.apache.commons.lang3.event #提供一些有用的基于事件的实用程序。
org.apache.commons.lang3.exception #提供异常功能。
org.apache.commons.lang3.math #为商业数学类扩展java.math。
org.apache.commons.lang3.mutable #为基元值和对象提供类型化的可变包装器。
org.apache.commons.lang3.reflect #提供反射java.lang.reflect API的常见高级用法。
org.apache.commons.lang3.text #提供用于处理和操作文本的类,部分用作java.text的扩展。
org.apache.commons.lang3.text.translate #用于从一组较小的构造块创建文本转换例程的API。
org.apache.commons.lang3.time #提供处理日期和持续时间的类和方法。
org.apache.commons.lang3.tuple #元组类,从版本3.0中的对类开始。
包介绍
StringUtils
判断字符串状态
isEmpty(String str)
:检查字符串是否为空(null 或长度为0)。isNotEmpty(String str)
:检查字符串是否不为空。isBlank(String str)
:检查字符串是否为空或只包含空格字符(对于制表符、换行符、换页符和回车符都会被判定为空)。isNotBlank(String str)
:检查字符串是否不为空且不仅包含空格字符。
字符串比较
equals(CharSequence str1, CharSequence str2)
:比较两个字符串是否相等,可以处理 null。equalsIgnoreCase(CharSequence str1, CharSequence str2)
:忽略大小写比较字符串是否相等。
字符串处理
trim(String str)
:去除字符串两端的空格。strip(String str)
:Java 11+ 新增,去除字符串两端的空格(包括 Unicode 空格字符)。substring(String str, int start, int end)
:提取字符串的子串。split(String str, char separatorChar)
:根据指定字符分割字符串为数组。join(CharSequence separator, CharSequence... elements)
:将一组元素连接成一个字符串,可以指定连接符。chop(String str)
:用于删除字符串的最后一个字符,无论该字符是什么。它返回一个新的字符串,该字符串是去除最后一个字符后的结果。chomp(String str)
:用于删除字符串末尾的换行符("\n")或回车符("\r")以及它们的组合("\r\n")。它返回一个新的字符串,该字符串是去除这些行尾字符后的结果。
字符串转换
upperCase(String str)
:将字符串转换为大写。lowerCase(String str)
:将字符串转换为小写。capitalize(String str)
:将字符串的第一个字符转换为大写。uncapitalize(String str)
:将字符串的第一个字符转换为小写。
字符串查找和替换
contains(CharSequence seq, CharSequence searchSeq)
:检查字符串是否包含指定的子串。indexOf(CharSequence seq, int searchChar)
:查找字符在字符串中的位置。replace(String text, String searchString, String replacement)
:替换字符串中的指定子串。
其他功能
defaultIfNull(String str, String defaultStr)
:如果字符串为 null,返回默认值。abbreviate(String str, int maxWidth)
:缩短字符串,以适应指定的最大宽度。countMatches(String str, String sub)
:用于计算一个字符串中指定子字符串出现的次数。getLevenshteinDistance(CharSequence str1, CharSequence str2)
:用于计算两个字符串之间的莱文斯坦编辑距离(Levenshtein Distance,添加一个字符、删除一个字符、替换一个字符),也称为编辑距离。莱文斯坦编辑距离是一种用于衡量两个字符串之间差异的度量方法,表示将一个字符串转换为另一个字符串所需的最少编辑操作次数。
测试类:
public class TestStringUtils {
@Test
public void test() {
System.out.println(StringUtils.isEmpty("")); // true
System.out.println(StringUtils.isEmpty(" ")); // false
System.out.println(StringUtils.isBlank(" ")); // true
System.out.println(StringUtils.abbreviate("12312346", 6)); // 123...
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
System.out.println(StringUtils.join(list, ';')); // 1;2;3;4;5;6
String input = "Hello, World!";
String choppedString = StringUtils.chop(input);
System.out.println("Input String: " + input); // Input String: Hello, World!
System.out.println("Chopped String: " + choppedString); // Chopped String: Hello, World
String text = "The quick brown fox jumps over the lazy dog. The dog is very lazy.";
String target = "lazy";
int count = StringUtils.countMatches(text, target);
System.out.println("Count: " + count); // 2
System.out.println(StringUtils.getLevenshteinDistance("elephant", "hippo")); //7
}
}