Overview of Java main() method
Find if String is empty by checking the length
It's the easiest and popular method to verify if String is empty or not. You can find length of String by calling length() method which actually returns number of characters in String. Be careful to check if String is null before calling length()to avoid NullPointerException
.
if(string != null && string.length() == 0){ return true; }
Using String isBlank() Method
This method returns true if the given string is empty or contains only white space code points, otherwise false.
It uses Character.isWhitespace(char) method to determine a white space character.
Using Java 5 and Below
boolean isEmptyString(String string) {
return string == null || string.length() == 0;
}
Blank Strings
Both String.isEmpty and String.length can be used to check for empty strings.
Using Java 6 and Above
If we also want to detect blank strings, we can achieve this with the help of String#trim. It will remove all leading and trailing whitespaces before performing the check.
if(str != null && !str.trim().isEmpty())
You can leverage Apache Commons StringUtils.isEmpty(str), which checks for empty strings and handles null gracefully. System.out.println(Strings.isNullOrEmpty("")); // true System.out.println(Strings.isNullOrEmpty(null)); // true
This way you check if the string is not null and not empty, also considering the empty spaces:
boolean isEmpty = str == null || str.trim().length() == 0;
if (isEmpty) {
// handle the validation
}
Using import com.google.common.base
if(!Strings.isNullOrEmpty(String str)) {
// Do your stuff here
}
Using Apache Commons
<dependency >
<groupId >org.apache.commons </groupId >
<artifactId >commons-lang3 </artifactId >
<version >3.11 </version >
</dependency >
Apache Commons has the StringUtils class which has a method to check blank or empty string
StringUtils.isBlank(string)
StringUtils.isEmpty(string)
Check if String with spaces is Empty or Null
public class StringNullOrBlank {
public static void main(String[] args) {
String str1 = null;
String str2 = " ";
if (isNullOrBlank(str1)) System.out.println("str1 is null or blank.");
else System.out.println("str1 is not null or blank.");
if (isNullOrBlank(str2)) System.out.println("str2 is null or blank.");
else System.out.println("str2 is not null or blank.");
}
public static boolean isNullOrBlank(String str) {
if (str != null && !str.trim().isEmpty()) return false;
return true;
}
}
output:
str1 is null or blank.
str2 is null or blank.
Using Guava
<dependency >
<groupId >com.google.guava </groupId >
<artifactId >guava </artifactId >
<version >29.0-jre </version >
</dependency >
Guavas Strings class comes with a method Strings.isNullOrEmpty
Strings.isNullOrEmpty(string)
It checks whether a given string is null or empty, but it will not check for whitespace-only strings.
String empty using the equals method
You can also compare String to empty String literal "" to check if it’s empty or not. equals method in Java returns false if another argument is null, so it automatically checks for the null string as well
public static boolean isStringEmptyByEquals(String input){
return "".equals(input);
}
In this article, we have seen Check blank or empty string in Java.
0 Comments
Post a Comment