Java | Apache Commons StringUtils
The Apache Commons library is utility library provides many features which lacks in existing Java Core Framework. It is widely used open sourced library.
In this post, we are going to learn utility methods provided by StringUtils class.
Unlike Java's String class, Apache StringUtils class methods are null-safe. This means no NullPointerExeception is thrown if null value is sent.
Below are popular methods used - Complete list of methods can be found at Apache commons StringUtils methods
StringUtils.isEmpty() / StringUtils.isBlank()
This methods used to check if a String contains any text. Both of these return true if the String is empty. The method isBlank() will return true if a String contains only whitespaces.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Checks if a CharSequence is empty ("") or null. | |
System.out.println(StringUtils.isEmpty(null)); //StringUtils.isEmpty(null) -> returns true | |
System.out.println(StringUtils.isEmpty("")); //StringUtils.isEmpty(null) -> returns true | |
System.out.println(StringUtils.isEmpty(" ")); //StringUtils.isEmpty(null) -> returns false | |
// Checks if a CharSequence is empty (""), null or whitespace only. | |
System.out.println(StringUtils.isBlank(null)); //StringUtils.isBlank(null) -> returns true | |
System.out.println(StringUtils.isBlank("")); //StringUtils.isBlank("") -> returns true | |
System.out.println(StringUtils.isBlank(" ")); //StringUtils.isBlank(" ") -> returns true | |
System.out.println(StringUtils.isBlank("LOREM")); //StringUtils.isBlank("LOREM") -> returns false |
StringUtils.isNotBlank()
Checks if a string is not empty (""), not null and not whitespace only.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.out.println(StringUtils.isNotBlank(null)); //StringUtils.isNotBlank(null) -> returns false | |
System.out.println(StringUtils.isNotBlank("")); //StringUtils.isNotBlank("") -> returns false | |
System.out.println(StringUtils.isNotBlank(" ")); //StringUtils.isNotBlank(" ") -> returns false | |
System.out.println(StringUtils.isNotBlank("LOREM")); //StringUtils.isNotBlank("LOREM") -> returns true |
StringUtils.equals()
This case sensitive method compares two strings, returning true if they represent equal sequences of characters. Two null references are considered to be equal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compares two CharSequences, returning true if they represent equal sequences of characters. | |
System.out.println(StringUtils.equals(null, null)); //StringUtils.equals(null, null) -> returns true | |
System.out.println(StringUtils.equals(null, "")); //StringUtils.equals(null, "") -> returns false | |
System.out.println(StringUtils.equals("", "")); //StringUtils.equals("", "") -> returns true | |
System.out.println(StringUtils.equals(" ", " ")); //StringUtils.equals(" ", " ") -> returns true | |
System.out.println(StringUtils.equals("LOREM", "LOREM")); //StringUtils.equals("LOREM", "LOREM") -> returns true |
StringUtils.startsWith
This case sensitive method checks of string starts with a specified prefix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check if a CharSequence starts with a specified prefix. | |
System.out.println(StringUtils.startsWith(null, null)); //StringUtils.startsWith(null, null) -> returns true | |
System.out.println(StringUtils.startsWith("LOREM", "LOREM")); //StringUtils.startsWith("LOREM", "LOREM") -> returns true |
StringUtils.endsWith
This case sensitive method checks of string ends with a specified prefix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check if a CharSequence ends with a specified prefix. | |
System.out.println(StringUtils.endsWith(null, null)); //StringUtils.endsWith(null, null) -> returns true | |
System.out.println(StringUtils.endsWith("LOREM", "LOREM")); //StringUtils.endsWith("LOREM", "LOREM") -> returns true | |
System.out.println(StringUtils.endsWith("LOREM", "REM")); //StringUtils.endsWith("LOREM", "REM") -> returns true |
StringUtils.indexOf()
This method try to search for first index of specified search text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Finds the first index within a CharSequence, handling null. This method uses String.indexOf(String, int) if possible. | |
System.out.println(StringUtils.indexOf(null, null)); //StringUtils.indexOf(null, null) -> returns -1 | |
System.out.println(StringUtils.indexOf("LOREM", "LOREM")); //StringUtils.indexOf("LOREM", "LOREM") -> returns 0 | |
System.out.println(StringUtils.indexOf("LOREM", "REM")); //StringUtils.indexOf("LOREM", "REM") -> returns 2 |
StringUtils.lastIndexOf()
This method try to search for last index of specified search text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Finds the last index within a CharSequence, handling null. This method uses String.lastIndexOf(String) if possible.
System.out.println(StringUtils.lastIndexOf(null, null)); //StringUtils.indexOf(null, null) -> returns -1
System.out.println(StringUtils.lastIndexOf("LOREM", "LOREM")); //StringUtils.indexOf("LOREM", "LOREM") -> returns 0
System.out.println(StringUtils.lastIndexOf("ababab", "ab")); //StringUtils.indexOf("ababab", "ab") -> returns 4
StringUtils.containsAny()
This method try to search for last index of specified search text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Finds the last index within a CharSequence, handling null. This method uses String.lastIndexOf(String) if possible. | |
System.out.println(StringUtils.lastIndexOf(null, null)); //StringUtils.indexOf(null, null) -> returns -1 | |
System.out.println(StringUtils.lastIndexOf("LOREM", "LOREM")); //StringUtils.indexOf("LOREM", "LOREM") -> returns 0 | |
System.out.println(StringUtils.lastIndexOf("ababab", "ab")); //StringUtils.indexOf("ababab", "ab") -> returns 4 |
This method try to check if string contains any input from array of strings. This is not case sensitive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Checks if the CharSequence contains any character in the given set of characters. | |
System.out.println(StringUtils.containsAny(null, '*')); //StringUtils.containsAny(null, *) -> returns false | |
System.out.println(StringUtils.containsAny("LOREM", "LOREM")); //StringUtils.containsAny("LOREM", "LOREM") -> returns true | |
System.out.println(StringUtils.containsAny("ababab", "ab")); //StringUtils.containsAny(ababab, "ab") -> returns true | |
System.out.println(StringUtils.containsAny("ababab", "AB")); //StringUtils.containsAny(ababab, "AB") -> returns false |
StringUtils.substring()
This method will return substring of given string and index. If index is negative(-) it will start from end.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Gets a substring from the specified String avoiding exceptions. | |
System.out.println(StringUtils.substring(null, 1)); //StringUtils.substring(null, 1) -> returns null | |
System.out.println(StringUtils.substring("LOREM", 1)); //StringUtils.substring("LOREM", 1) -> returns OREM | |
System.out.println(StringUtils.substring("ababab", 1)); //StringUtils.substring(ababab, 1) -> returns babab | |
System.out.println(StringUtils.substring("ababab", -2)); //StringUtils.substring(ababab, -2) -> returns ab |
StringUtils.split()
This method splits the provided string into an array, using whitespace or any separator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Splits the provided text into an array, using whitespace/ the separator. | |
System.out.println(StringUtils.split(null)); //StringUtils.split(null, 1) -> returns null | |
System.out.println(StringUtils.split(null, "-")); //StringUtils.split(null, "-") -> returns OREM | |
System.out.println(Arrays.toString(StringUtils.split(" ababab "))); //StringUtils.split(" ababab ") -> returns [ababab] | |
System.out.println(Arrays.toString(StringUtils.split("ab-cd-ef", "-"))); //StringUtils.split("ab-ab-ab", "-") -> returns [ab, cd, ef] |
StringUtils.replace()
This method will replace string with given replacement text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Replaces all occurrences of a String within another String. | |
System.out.println(StringUtils.replace("LOREM", null, null)); //StringUtils.replace("LOREM", null, null) -> returns LOREM | |
System.out.println(StringUtils.replace("LOREM", "LO", "AB")); //StringUtils.replace("LOREM", "LO", "AB") -> returns ABREM |
StringUtils.join()
This method will join given array input with given joining delimiter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Joins the elements of the provided array into a single String containing the provided list of elements. | |
System.out.println(StringUtils.join(new String[]{"ab", "cd", "ef"},'-')); //StringUtils.join(new String[]{"1", "2", "3"},'-') -> returns ab-cd-ef | |
Using apache commons in your project.
Maven link - Maven Repository
Maven/ Gradle dependency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-lang3</artifactId> | |
<version>3.12.0</version> | |
</dependency> | |
// Gradle Dependency | |
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' |
Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Checks if a CharSequence is empty ("") or null. | |
System.out.println(StringUtils.isEmpty(null)); //StringUtils.isEmpty(null) -> returns true | |
System.out.println(StringUtils.isEmpty("")); //StringUtils.isEmpty(null) -> returns true | |
System.out.println(StringUtils.isEmpty(" ")); //StringUtils.isEmpty(null) -> returns false | |
// Checks if a CharSequence is empty (""), null or whitespace only. | |
System.out.println(StringUtils.isBlank(null)); //StringUtils.isBlank(null) -> returns true | |
System.out.println(StringUtils.isBlank("")); //StringUtils.isBlank("") -> returns true | |
System.out.println(StringUtils.isBlank(" ")); //StringUtils.isBlank(" ") -> returns true | |
System.out.println(StringUtils.isBlank("LOREM")); //StringUtils.isBlank("LOREM") -> returns false | |
// Checks if a CharSequence is not empty (""), not null and not whitespace only. | |
System.out.println(StringUtils.isNotBlank(null)); //StringUtils.isNotBlank(null) -> returns false | |
System.out.println(StringUtils.isNotBlank("")); //StringUtils.isNotBlank("") -> returns false | |
System.out.println(StringUtils.isNotBlank(" ")); //StringUtils.isNotBlank(" ") -> returns false | |
System.out.println(StringUtils.isNotBlank("LOREM")); //StringUtils.isNotBlank("LOREM") -> returns true | |
// Compares two CharSequences, returning true if they represent equal sequences of characters. | |
System.out.println(StringUtils.equals(null, null)); //StringUtils.equals(null, null) -> returns true | |
System.out.println(StringUtils.equals(null, "")); //StringUtils.equals(null, "") -> returns false | |
System.out.println(StringUtils.equals("", "")); //StringUtils.equals("", "") -> returns true | |
System.out.println(StringUtils.equals(" ", " ")); //StringUtils.equals(" ", " ") -> returns true | |
System.out.println(StringUtils.equals("LOREM", "LOREM")); //StringUtils.equals("LOREM", "LOREM") -> returns true | |
// Check if a CharSequence starts with a specified prefix. | |
System.out.println(StringUtils.startsWith(null, null)); //StringUtils.startsWith(null, null) -> returns true | |
System.out.println(StringUtils.startsWith("LOREM", "LOREM")); //StringUtils.startsWith("LOREM", "LOREM") -> returns true | |
// Check if a CharSequence ends with a specified prefix. | |
System.out.println(StringUtils.endsWith(null, null)); //StringUtils.endsWith(null, null) -> returns true | |
System.out.println(StringUtils.endsWith("LOREM", "LOREM")); //StringUtils.endsWith("LOREM", "LOREM") -> returns true | |
System.out.println(StringUtils.endsWith("LOREM", "REM")); //StringUtils.endsWith("LOREM", "REM") -> returns true | |
// Finds the first index within a CharSequence, handling null. This method uses String.indexOf(String, int) if possible. | |
System.out.println(StringUtils.indexOf(null, null)); //StringUtils.indexOf(null, null) -> returns -1 | |
System.out.println(StringUtils.indexOf("LOREM", "LOREM")); //StringUtils.indexOf("LOREM", "LOREM") -> returns 0 | |
System.out.println(StringUtils.indexOf("LOREM", "REM")); //StringUtils.indexOf("LOREM", "REM") -> returns 2 | |
// Finds the last index within a CharSequence, handling null. This method uses String.lastIndexOf(String) if possible. | |
System.out.println(StringUtils.lastIndexOf(null, null)); //StringUtils.indexOf(null, null) -> returns -1 | |
System.out.println(StringUtils.lastIndexOf("LOREM", "LOREM")); //StringUtils.indexOf("LOREM", "LOREM") -> returns 0 | |
System.out.println(StringUtils.lastIndexOf("ababab", "ab")); //StringUtils.indexOf("ababab", "ab") -> returns 4 | |
// Checks if the CharSequence contains any character in the given set of characters. | |
System.out.println(StringUtils.containsAny(null, '*')); //StringUtils.containsAny(null, *) -> returns false | |
System.out.println(StringUtils.containsAny("LOREM", "LOREM")); //StringUtils.containsAny("LOREM", "LOREM") -> returns true | |
System.out.println(StringUtils.containsAny("ababab", "ab")); //StringUtils.containsAny(ababab, "ab") -> returns true | |
System.out.println(StringUtils.containsAny("ababab", "AB")); //StringUtils.containsAny(ababab, "AB") -> returns false | |
// Gets a substring from the specified String avoiding exceptions. | |
System.out.println(StringUtils.substring(null, 1)); //StringUtils.substring(null, 1) -> returns null | |
System.out.println(StringUtils.substring("LOREM", 1)); //StringUtils.substring("LOREM", 1) -> returns OREM | |
System.out.println(StringUtils.substring("ababab", 1)); //StringUtils.substring(ababab, 1) -> returns babab | |
System.out.println(StringUtils.substring("ababab", -2)); //StringUtils.substring(ababab, -2) -> returns ab | |
// Splits the provided text into an array, using whitespace/ the separator. | |
System.out.println(StringUtils.split(null)); //StringUtils.split(null, 1) -> returns null | |
System.out.println(StringUtils.split(null, "-")); //StringUtils.split(null, "-") -> returns OREM | |
System.out.println(Arrays.toString(StringUtils.split(" ababab "))); //StringUtils.split(" ababab ") -> returns [ababab] | |
System.out.println(Arrays.toString(StringUtils.split("ab-cd-ef", "-"))); //StringUtils.split("ab-ab-ab", "-") -> returns [ab, cd, ef] | |
// Replaces all occurrences of a String within another String. | |
System.out.println(StringUtils.replace("LOREM", null, null)); //StringUtils.replace("LOREM", null, null) -> returns LOREM | |
System.out.println(StringUtils.replace("LOREM", "LO", "AB")); //StringUtils.replace("LOREM", "LO", "AB") -> returns ABREM | |
// Joins the elements of the provided array into a single String containing the provided list of elements. | |
System.out.println(StringUtils.join(new String[]{"ab", "cd", "ef"},'-')); //StringUtils.join(new String[]{"1", "2", "3"},'-') -> returns ab-cd-ef | |
Comments
Post a Comment