In this blog we will learn how to use StringUtils library in Java.
StringUtils is a part oF Apache commons lang library.
But why to choose ‘StringUtils’ over built-in ‘String’ class?
As a developer here are some reasons why you might choose to use StringUtils
from Apache Commons Lang over the built-in String class in your project
String Manipulation made easy: StringUtils offers a variety of simple methods for string manipulation
that are not present in the standard String class.
These include operations like truncating, padding, capitalization etc. You do not have to write your own methods and worry about bugs.
Avoid NullPointerExcpetion: StringUtils provides methods to handle null values safely.
You don’t have to worry about NullPointerException in your project. You can just relax.
Whitespace Handling is super simple: StringUtils offers methods to manipulate whitespace in strings,
such as removing leading and trailing spaces etc
Providing Default Values: StringUtils allow you to provide default values in case a given string is null or empty,
making code more readable.
String Joining with Custom Delimiters: StringUtils offers methods for joining and splitting strings
with custom delimiters or separators.
Handling Empty/String Checks: StringUtils has methods to super convenient to check for empty, blank,
or non-empty strings without having to write your own code.
Abbreviation and Ellipsis: StringUtils offers convenient methods to abbreviate or add ellipses to strings,
which can be useful for displaying truncated strings in our code.
Let’s get started!
We first need to add maven dependency for StringUtils from Apache commons library
StringUtils.isBlank
This method checks if the provided string is null, has a length of 0, or consists only of whitespace characters.
Here whitespace character is also considered blank
This method checks if the provided string is null or has a length of 0.
It considers a non-null, non-empty string as not empty.
Whitespace characters (spaces, tabs, line breaks) are considered as content,
and a string containing only whitespace characters is not considered empty.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
importorg.apache.commons.lang3.StringUtils;publicclassApp{publicstaticvoidmain(String[]args){System.out.println(StringUtils.isEmpty(null));//true
System.out.println(StringUtils.isEmpty(""));//true
System.out.println(StringUtils.isEmpty(" "));// false whitespace is content
System.out.println(StringUtils.isEmpty("Hello World"));//false
System.out.println(StringUtils.isEmpty("\n"));//false .new line is content
System.out.println(StringUtils.isEmpty(" \t"));//false . tab is content
}}
StringUtils.isNotEmpty()
This method checks if the provided string is not null or has a length greater than 0.
It is just opposite of isEmpty() method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
importorg.apache.commons.lang3.StringUtils;publicclassApp{publicstaticvoidmain(String[]args){System.out.println(StringUtils.isNotEmpty(null));//false
System.out.println(StringUtils.isNotEmpty(""));//false
System.out.println(StringUtils.isNotEmpty(" "));// true whitespace is content
System.out.println(StringUtils.isNotEmpty("Hello World"));//true
System.out.println(StringUtils.isNotEmpty("\n"));//true .new line is content
System.out.println(StringUtils.isNotEmpty(" \t"));//true . tab is content
}}
StringUtils.containsAny()
containsAny() method checks if a given string contains any of the character in the given set.
Character set to be tested can be passed as arguments to the method.
The StringUtils.abbreviate method is used to abbreviate a string if it’s longer than a specified maximum length.
This can be useful when you want to display a short version of a string while preserving its content.
If the string’s length exceeds the specified maximum length,
it will be shortened and an ellipsis ("…") will be added to indicate that the string has been abbreviated.
1
2
3
4
5
6
7
8
9
10
importorg.apache.commons.lang3.StringUtils;publicclassApp{publicstaticvoidmain(String[]args){System.out.println(StringUtils.abbreviate("hello world is fine",14));// hello world...
}}
You can add your own marker by specifying it in the argument as shown below.
1
2
3
4
5
6
7
8
9
10
11
importorg.apache.commons.lang3.StringUtils;publicclassApp{publicstaticvoidmain(String[]args){System.out.println(StringUtils.abbreviate("hello world is fine","*",14));// hello world i*
}}
StringUtils.join()
StringUtils.join() is used to concatenate elements of an array, collection into a single string, using a specified delimiter.
importorg.apache.commons.lang3.StringUtils;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;publicclassApp{publicstaticvoidmain(String[]args){// Join elements of an array using a delimiter
String[]fruits={"apple","banana","cherry"};StringjoinedArray=StringUtils.join(fruits,", ");System.out.println("Joined array: "+joinedArray);// Join elements of a collection using a delimiter
List<String>colors=newArrayList<>(Arrays.asList("red","green","blue"));StringjoinedCollection=StringUtils.join(colors," | ");System.out.println("Joined collection: "+joinedCollection);}}