Java String isEmpty() Method
The isEmpty() method is used to determine whether a string is empty.
Syntax
public boolean isEmpty()
Parameters
- None
Return Value
Returns true if the string is empty, otherwise returns false.
The string islength()method calculates the length of the string; if it returns 0, it is an empty string.
Example
The following example demonstrates the use of the isEmpty() method:
Example
public class Main {
public static void main(String[] args) {
String myStr1 = "Example";
String myStr2 = ""; //Empty string
String myStr3 = " "; //Multiple spaces, length() is not 0
System.out.println("Is myStr1 empty?" + myStr1.isEmpty());
System.out.println("Is myStr2 empty?" + myStr2.isEmpty());
System.out.println("Length of myStr3:" + myStr3.length());
System.out.println("Is myStr3 empty?" + myStr3.isEmpty());
}
}
The output of the above program is:
myStr1 是否为空:false myStr2 是否为空:true myStr3 长度:4 myStr3 是否为空:false
Other extensions
Java String Class