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.

// 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.

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
view raw IsNotBlank.java hosted with ❤ by GitHub

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.

// 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
view raw Equals.java hosted with ❤ by GitHub

StringUtils.startsWith 

This case sensitive method checks of string starts with a specified prefix.

// 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
view raw StartsWith.java hosted with ❤ by GitHub

StringUtils.endsWith

This case sensitive method checks of string ends with a specified prefix.

// 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
view raw EndsWith.java hosted with ❤ by GitHub

StringUtils.indexOf()

This method try to search for first index of specified search text.

// 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
view raw IndexOf.java hosted with ❤ by GitHub

StringUtils.lastIndexOf()

This method try to search for last index of specified search text.

// 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 check if string contains any input from array of strings. This is not case sensitive.

// 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.

// 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
view raw Substring.java hosted with ❤ by GitHub

StringUtils.split()

This method splits the provided string into an array, using whitespace or any separator.

// 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]
view raw Split.java hosted with ❤ by GitHub

StringUtils.replace()

This method will replace string with given replacement text.

// 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
view raw Replace.java hosted with ❤ by GitHub

StringUtils.join()

This method will join given array input with given joining delimiter.

// 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
view raw Join.java hosted with ❤ by GitHub

Using apache commons in your project.

Maven link - Maven Repository

Maven/ Gradle dependency

<!-- 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

// 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

Popular posts from this blog

Spring | Using TIBCO EMS with Spring framework

TIBCO | For Loop - Accumulate output

TIBCO | JNDI Server & JMS Instance creation