String Processing with Apache Commons Lang 3 – 用Apache Commons Lang 3处理字符串

最后修改: 2017年 3月 9日

1. Overview

1.概述

The Apache Commons Lang 3 library provides support for manipulation of core classes of the Java APIs. This support includes methods for handling strings, numbers, dates, concurrency, object reflection and more.

Apache Commons Lang 3库为操作Java APIs的核心类提供支持。这种支持包括用于处理字符串、数字、日期、并发、对象反射等的方法。

In addition to providing a general introduction to the library, this tutorial demonstrates methods of the StringUtils class which is used for manipulation of String instances.

除了提供库的一般介绍外,本教程还演示了StringUtils类的方法,该类用于操作String实例。

2. Maven Dependency

2.Maven的依赖性

In order to use the Commons Lang 3 library, just pull it from the central Maven repository using the following dependency:

为了使用Commons Lang 3库,只需使用以下依赖关系从Maven中心仓库拉取即可。

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

You can find the latest version of this library here.

你可以找到这个库的最新版本这里

3. StringUtils

3.StringUtils

The StringUtils class provides methods for null-safe operations on strings.

StringUtils类提供了对字符串进行null安全操作的方法。

Many methods of this class have corresponding ones defined in class java.lang.String, which are not null-safe. However, this section will instead focus on several methods that do not have equivalents in the String class.

这个类的许多方法在java.lang.String类中都有相应的定义,它们不是null安全的。然而,本节将重点讨论几个在String类中没有对应的方法。

4. The containsAny Method

4、containsAny方法

The containsAny method checks if a given String contains any character in the given set of characters. This set of characters can be passed in the form of a String or char varargs.

containsAny方法检查一个给定的String是否包含给定字符集中的任何字符。这组字符可以以Stringchar varargs的形式传递。

The following code fragment demonstrates the use of two overloaded flavors of this method with result verification:

下面的代码片段演示了这个方法的两个重载版本的使用和结果验证。

String string = "baeldung.com";
boolean contained1 = StringUtils.containsAny(string, 'a', 'b', 'c');
boolean contained2 = StringUtils.containsAny(string, 'x', 'y', 'z');
boolean contained3 = StringUtils.containsAny(string, "abc");
boolean contained4 = StringUtils.containsAny(string, "xyz");
 
assertTrue(contained1);
assertFalse(contained2);
assertTrue(contained3);
assertFalse(contained4);

5. The containsIgnoreCase Method

5.containsIgnoreCase 方法

The containsIgnoreCase method checks if a given String contains another String in a case insensitive manner.

containsIgnoreCase方法检查一个给定的String是否以不区分大小写的方式包含另一个String

The following code fragment verifies that the String “baeldung.com” comprises “BAELDUNG” when upper and lower case is ignored:

下面的代码片段验证了字符串 “baeldung.com”包括“BAELDUNG”,当大写和小写被忽略时。

String string = "baeldung.com";
boolean contained = StringUtils.containsIgnoreCase(string, "BAELDUNG");
 
assertTrue(contained);

6. The countMatches Method

6.countMatches方法

The counterMatches method counts how many times a character or substring appears in a given String.

counterMatches方法计算一个字符或子串在给定的String.中出现多少次。

The following is a demonstration of this method, confirming that ‘w’ appears four times and “com” does twice in the String “welcome to www.baeldung.com”:

下面是这个方法的演示,确认‘w’出现四次,“com”String“welcome to www.baeldung.com”出现两次。

String string = "welcome to www.baeldung.com";
int charNum = StringUtils.countMatches(string, 'w');
int stringNum = StringUtils.countMatches(string, "com");
 
assertEquals(4, charNum);
assertEquals(2, stringNum);

7. Appending and Prepending Method

7.附加和预设方法

The appendIfMissing and appendIfMissingIgnoreCase methods append a suffix to the end of a given String if it does not already end with any of the passed-in suffixes in a case sensitive and insensitive manner respectively.

appendIfMissingappendIfMissingIgnoreCase方法在给定的String的末尾添加一个后缀,如果它还没有以任何传入的后缀结尾,则分别以敏感和不敏感的方式添加。

Similarly, the prependIfMissing and prependIfMissingIgnoreCase methods prepend a prefix to the beginning of a given String if it does not start with any of the passed-in prefixes.

类似地,prependIfMissingprependIfMissingIgnoreCase方法在给定的String的开头不以任何传入的前缀开始时,将前缀加入。

In the following example, the appendIfMissing and prependIfMissing methods are used to add a suffix and prefix to the String “baeldung.com” without these affixes being repeated:

在下面的例子中,appendIfMissingprependIfMissing方法被用来向字符串 “baeldung.com”添加后缀和前缀,而这些后缀不会被重复。

String string = "baeldung.com";
String stringWithSuffix = StringUtils.appendIfMissing(string, ".com");
String stringWithPrefix = StringUtils.prependIfMissing(string, "www.");
 
assertEquals("baeldung.com", stringWithSuffix);
assertEquals("www.baeldung.com", stringWithPrefix);

8. Case Changing Method

8.换案方法

The String class already defines methods to convert all characters of a String to uppercase or lowercase. This subsection only illustrates the use of methods changing the case of a String in other ways, including swapCase, capitalize and uncapitalize.

String类已经定义了将String的所有字符转换为大写或小写的方法。本小节只说明了以其他方式改变String大小写的方法的使用,包括swapCasecapitalizeuncapitalize

The swapCase method swaps the case of a String, changing uppercase to lowercase and lowercase to uppercase:

swapCase方法交换了字符串的大小写,将大写变成小写,小写变成大写。

String originalString = "baeldung.COM";
String swappedString = StringUtils.swapCase(originalString);
 
assertEquals("BAELDUNG.com", swappedString);

The capitalize method converts the first character of a given String to uppercase, leaving all remaining characters unchanged:

capitalize方法将给定String的第一个字符转换为大写字母,其余所有字符保持不变。

String originalString = "baeldung";
String capitalizedString = StringUtils.capitalize(originalString);
 
assertEquals("Baeldung", capitalizedString);

The uncapitalize method converts the first character of the given String to lowercase, leaving all remaining characters unchanged:

uncapitalize方法将给定String的第一个字符转换为小写,其余所有字符保持不变。

String originalString = "Baeldung";
String uncapitalizedString = StringUtils.uncapitalize(originalString);
 
assertEquals("baeldung", uncapitalizedString);

9. Reversing Method

9.逆转法

The StringUtils class defines two methods for reversing strings: reverse and reverseDelimited. The reverse method rearranges all characters of a String in the opposite order, while the reverseDelimited method reorders groups of characters, separated by a specified delimiter.

StringUtils类定义了两个用于反转字符串的方法。reversereverseDelimitedreverse方法将String的所有字符以相反的顺序重新排列,而reverseDelimited方法将字符组重新排列,以指定的分隔符分开。

The following code fragment reverses the string “baeldung” and validates the outcome:

下面的代码片段反转了字符串“baeldung”并验证了结果。

String originalString = "baeldung";
String reversedString = StringUtils.reverse(originalString);
 
assertEquals("gnudleab", reversedString);

With the reverseDelimited method, characters are reversed in groups instead of individually:

使用reverseDelimited方法,字符被成组反转,而不是单独反转。

String originalString = "www.baeldung.com";
String reversedString = StringUtils.reverseDelimited(originalString, '.');
 
assertEquals("com.baeldung.www", reversedString);

10. The rotate() Method

10.rotate()方法

The rotate() method circularly shifts characters of a String a number of positions. The code fragment below moves all characters of the String “baeldung” four positions to the right and verifies the result:

rotate()方法将字符串的字符循环移动若干个位置。下面的代码片段将字符串 “baeldung”的所有字符向右移动了四个位置并验证了结果。

String originalString = "baeldung";
String rotatedString = StringUtils.rotate(originalString, 4);
 
assertEquals("dungbael", rotatedString);

11. The difference Method

11.差别法

The difference method compares two strings, returning the remainder of the second String, starting from the position where it is different from the first. The following code fragment compares two Strings: “Baeldung Tutorials” and “Baeldung Courses” in both directions and validates the outcome:

difference方法比较两个字符串,返回第二个字符串的剩余部分,从它与第一个不同的位置开始。下面的代码片段比较了两个字符串。”Baeldung Tutorials”“Baeldung Courses”在两个方向上进行比较,并验证其结果。

String tutorials = "Baeldung Tutorials";
String courses = "Baeldung Courses";
String diff1 = StringUtils.difference(tutorials, courses);
String diff2 = StringUtils.difference(courses, tutorials);
 
assertEquals("Courses", diff1);
assertEquals("Tutorials", diff2);

12. Conclusion

12.结论

This tutorial introduces String processing in the Apache Commons Lang 3 and goes over the main APIs we can use out of the StringUtils library class.

本教程介绍了Apache Commons Lang 3中的字符串处理,并介绍了我们可以从StringUtils库类中使用的主要API。

As always, the implementation of all examples and code snippets given above can be found in the GitHub project.

一如既往,上面给出的所有例子和代码片段的实现都可以在GitHub项目中找到。