Member-only story

How do I use StringUtils Methods In Project ?

Gain Java Knowledge
4 min readDec 26, 2023

--

In this article, we will learn the most useful methods of stringUtils. String utils is a utility class from Apache common-lang package.

Latest version of apache commons lang library :


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>

we just need to add this maven dependency in our pom.xml and we can use stringUtils library all methods in our project.

Okay, Let’s look at some of the most useful methods of stringUtils and how they can be useful in our code.

  1. IsEmpty/IsBlank — checks if a String contains text
String test1 = "name";
boolean res1 = StringUtils.isEmpty(test1);
boolean res2 = StringUtils.isBlank(test1);
System.out.println("IsEmpty : "+res1+" ,isBlank : "+res2);
//IsEmpty : false ,isBlank : false

response for both methods will be same : true. But in case of isEmpty method will return true if string is null or empty(“”). But in case of isBlank method it will return true in case of string is null, empty(“”) or if contains white space(“ “) in all other cases it will return false.

2. Trim/Strip — removes leading and trailing whitespace

String test3 = " abc ";
String res3 = StringUtils.trim(test3);
String res4 = StringUtils.strip(test3);
System.out.println("trim:"+res3+",strip:"+res4);
//trim:abc,strip:abc

--

--

Gain Java Knowledge
Gain Java Knowledge

Written by Gain Java Knowledge

The Java programming language is one of the most popular languages today. Stay up to date with news, certifications, free learning resources and much more.

No responses yet