In this post, we are going to see about org.apache.commons.lang3.StringUtils isEmpty() and IsBlank() Example in Java. The isEmpty() method is used to check any string is empty or null. To check whitespace we can use IsBlank() method.
Introduction.
We need to add below dependency in maven to use org.apache.commons.lang3.StringUtils isEmpty() method. We can download apache-commons maven dependency from here.
pom.xml changes
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
StringUtils isEmpty() Example in Java.
The StringUtils isEmpty() is a static method which return type is boolean and accepts CharSequence as a parameter.
Syntax – public static boolean isEmpty(final CharSequence cs);
It returns true if any string is null or the length of the string is zero
Let’s see an isEmpty() Example in Java.
package com.javatute; import org.apache.commons.lang3.StringUtils; public class StringUtilsIsEmpty { public static void main(String[] args) { String s1 = ""; String s2 = null; System.out.println(StringUtils.isEmpty(s1)); // true System.out.println(StringUtils.isEmpty(s2)); // true } }
The output is –
true
true
The isEmpty() method doesn’t check for whitespace. It will return false if our string is whitespace.
package com.javatute; import org.apache.commons.lang3.StringUtils; public class StringUtilsIsEmpty { public static void main(String[] args) { String s1 = " "; System.out.println(StringUtils.isEmpty(s1)); // false } }
The output is – false
We need to use StringUtils isBlank() method to check whitespace.
The internal implementation of the isEmpty method().
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
StringUtils isBlank() Example in Java.
The StringUtils isBlank() is a static method which return type is boolean and accepts CharSequence as a parameter.
Syntax – public static boolean isBlank(final CharSequence cs);
It returns true if any string is null or the length of the string is zero string is whitespace.
Let’s see an isBlank() Example in Java.
package com.javatute.serviceimpl; import org.apache.commons.lang3.StringUtils; public class StringUtilsIsBlank { public static void main(String[] args) { String s1 = ""; String s2 = null; String s3 = " "; String s4 = "jack"; System.out.println(StringUtils.isBlank(s1)); // true System.out.println(StringUtils.isBlank(s2)); // true System.out.println(StringUtils.isBlank(s3)); // true System.out.println(StringUtils.isBlank(s4)); // false } }
The output is –
true
true
true
false
The internal implementation of the StringUtils isBlank() method.
public static boolean isBlank(final CharSequence cs) { int strLen; if (cs == null || (strLen = cs.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; }
Note – The isBlank() has been changed from isBlank(String) to isBlank(CharSequence) in apache commons 3.0 versions.
Difference between StringUtils isEmpty() and isBlank() methods.
package com.javatute; import org.apache.commons.lang3.StringUtils; public class StringUtilsIsEmptyVsIsBlank { public static void main(String[] args) { String s1 = ""; String s2 = null; String s3 = " "; System.out.println("Using StringUtils isEmpty() method"); System.out.println(StringUtils.isEmpty(s1)); // true System.out.println(StringUtils.isEmpty(s2)); // true System.out.println(StringUtils.isEmpty(s3)); // false System.out.println("Using StringUtils isBlank() method"); System.out.println(StringUtils.isBlank(s1)); // true System.out.println(StringUtils.isBlank(s2)); // true System.out.println(StringUtils.isBlank(s3)); // true } }
The output is –
Using StringUtils isEmpty() method
true
true
false
Using StringUtils isBlank() method
true
true
true
Note – The isEmpty() has been changed from isEmpty(String) to isEmpty(CharSequence) since apache commons 3.0 versions.
StringUtils isNotEmpty() Example in Java.
Let’s see an example of org.apache.commons.lang3.StringUtils.isNotEmpty() method example.
package com.javatute.serviceimpl; import org.apache.commons.lang3.StringUtils; public class StringUtlilsIsNotEmptyExample { public static void main(String[] args) { String s1 = ""; String s2 = null; String s3 = "some string"; String s4 = " "; // white space System.out.println(StringUtils.isNotEmpty(s1)); // false System.out.println(StringUtils.isNotEmpty(s2)); // false System.out.println(StringUtils.isNotEmpty(s3)); // true System.out.println(StringUtils.isNotEmpty(s4)); // true } }
Output is –
false
false
true
true
Note – StringUtils.isNotEmpty() internally uses isEmty() method.
StringUtils isNotBlank() Example in Java.
package com.javatute.serviceimpl; import org.apache.commons.lang3.StringUtils; public class StringUtlilsIsNotBlankExample { public static void main(String[] args) { String s1 = ""; String s2 = null; String s3 = "some string"; String s4 = " "; // white space System.out.println(StringUtils.isNotBlank(s1)); // false System.out.println(StringUtils.isNotBlank(s2)); // false System.out.println(StringUtils.isNotBlank(s3)); // true System.out.println(StringUtils.isNotBlank(s4)); // true } }
Output is –
false
false
true
false
That’s all about StringUtils isEmpty() and IsBlank() Example in Java.
You may like.
- String Constant pool in java.
- String compareTo() method in java.
- String to int in java
- How to convert Arraylist to array and array to ArrayList in Java
- How to make List, Set and Map Read Only in Java
- How to avoid duplicate elements in ArrayList
- Constructor chaining in java with example
- Static variable, method and block in java
- instance variable in java
- How ArrayList works internally in Java
- Difference between String and StringBuffer in java.
- Java String Programs With examples.
- How HashMap works internally in Java.
- How run() method Works internally in Java.
- Difference between HashMap and Hashtable in java
- Difference between Comparable and Comparator in java
- Difference between Iterator and ListIterator in Java
- Difference between Iterator and Enumeration in java.
- instanceof operator in java.
- String interview questions in java.
See Docs here.
Summary – We have seen StringUtils isEmpty() and IsBlank() Example in Java. StringUtils isEmpty() returns true if string is empty or null. To check whitespace we can use isBlank() method. We also covered isNotEmpty() and isNotBlank() methods.