xxxxxxxxxx
if (myString == null || myString.equals(""))
throw new IllegalArgumentException("empty string");
xxxxxxxxxx
if(str != null && !str.isEmpty()) { /* do your stuffs here */ }
xxxxxxxxxx
if(str != null && !str.isEmpty())
Be sure to use the parts of && in this order, because java will not proceed to evaluate the second part if the first part of && fails, thus ensuring you will not get a null pointer exception from str.isEmpty() if str is null.
xxxxxxxxxx
/* You can use Apache Commons */
StringUtils.isEmpty(str)
/* which checks for empty strings and handles null gracefully. */
/* HINT */
/* the shorter and simler your code, the easier it is to test it */
xxxxxxxxxx
let str1 = "Hello world!";
let str2 = "";
let str3 = 4;
console.log(str1.length === 0)
console.log(str2.length === 0)
console.log(str3.length === 0)
false
true
false
xxxxxxxxxx
public class Main {
public static void main(String[] args) {
// Example string
String str = null;
// Check if string is null
if (str == null) {
System.out.println("String is null");
} else {
System.out.println("String is not null");
}
}
}