Member-only story
How do I use StringUtils Methods In Project ?
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.
- 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