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.

StringUtils.isNotBlank()

Checks if a string is not empty (""), not null and not whitespace only.

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.

StringUtils.startsWith 

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

StringUtils.endsWith

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

StringUtils.indexOf()

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

StringUtils.lastIndexOf()

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


StringUtils.containsAny()

This method try to check if string contains any input from array of strings. This is not case sensitive.

StringUtils.substring()

This method will return substring of given string and index. If index is negative(-) it will start from end.

StringUtils.split()

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

StringUtils.replace()

This method will replace string with given replacement text.

StringUtils.join()

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

Using apache commons in your project.

Maven link - Maven Repository

Maven/ Gradle dependency

Examples

Comments

Popular posts from this blog

Spring | Using TIBCO EMS with Spring framework

TIBCO | For Loop - Accumulate output

TIBCO | JNDI Server & JMS Instance creation