Table of Contents
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.
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.
In order to use the Commons Lang 3 library, just pull it from the central Maven repository using the following dependency:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.11</version> </dependency>
You can find the latest version of this library here.
The StringUtils class provides methods for null-safe operations on strings.
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.
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.
The following code fragment demonstrates the use of two overloaded flavors of this method with result verification:
String string = "maixuanviet.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);
The containsIgnoreCase method checks if a given String contains another String in a case insensitive manner.
The following code fragment verifies that the String “maixuanviet.com” comprises “VIETMX” when upper and lower case is ignored:
String string = "maixuanviet.com"; boolean contained = StringUtils.containsIgnoreCase(string, "VIETMX"); assertTrue(contained);
The counterMatches method counts how many times a character or substring appears in a given 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.maixuanviet.com”:
String string = "welcome to www.maixuanviet.com"; int charNum = StringUtils.countMatches(string, 'a'); int stringNum = StringUtils.countMatches(string, "com"); assertEquals(4, charNum); assertEquals(2, stringNum);
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.
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.
In the following example, the appendIfMissing and prependIfMissing methods are used to add a suffix and prefix to the String “maixuanviet.com” without these affixes being repeated:
String string = "maixuanviet.com"; String stringWithSuffix = StringUtils.appendIfMissing(string, ".com"); String stringWithPrefix = StringUtils.prependIfMissing(string, "www."); assertEquals("maixuanviet.com", stringWithSuffix); assertEquals("www.maixuanviet.com", stringWithPrefix);
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.
The swapCase method swaps the case of a String, changing uppercase to lowercase and lowercase to uppercase:
String originalString = "maixuanviet.COM"; String swappedString = StringUtils.swapCase(originalString); assertEquals("MAIXUANVIET.com", swappedString);
The capitalize method converts the first character of a given String to uppercase, leaving all remaining characters unchanged:
String originalString = "maixuanviet"; String capitalizedString = StringUtils.capitalize(originalString); assertEquals("Maixuanviet", capitalizedString);
The uncapitalize method converts the first character of the given String to lowercase, leaving all remaining characters unchanged:
String originalString = "Maixuanviet"; String uncapitalizedString = StringUtils.uncapitalize(originalString); assertEquals("maixuanviet", uncapitalizedString);
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.
The following code fragment reverses the string “maixuanviet” and validates the outcome:
String originalString = "maixuanviet"; String reversedString = StringUtils.reverse(originalString); assertEquals("gnudleab", reversedString);
With the reverseDelimited method, characters are reversed in groups instead of individually:
String originalString = "www.maixuanviet.com"; String reversedString = StringUtils.reverseDelimited(originalString, '.'); assertEquals("com.maixuanviet.www", reversedString);
The rotate() method circularly shifts characters of a String a number of positions. The code fragment below moves all characters of the String “maixuanviet” four positions to the right and verifies the result:
String originalString = "maixuanviet"; String rotatedString = StringUtils.rotate(originalString, 4); assertEquals("dungbael", rotatedString);
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: “VietMX Tutorials” and “VietMX Courses” in both directions and validates the outcome:
String tutorials = "VietMX Tutorials"; String courses = "VietMX Courses"; String diff1 = StringUtils.difference(tutorials, courses); String diff2 = StringUtils.difference(courses, tutorials); assertEquals("Courses", diff1); assertEquals("Tutorials", diff2);
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.
As always, the implementation of all examples and code snippets given above can be found in the GitHub project.