📅  最后修改于: 2020-09-26 05:42:47             🧑  作者: Mango
java 字符串 isEmpty()方法检查此字符串是否为空。如果字符串的长度为0,则返回true,否则返回false。换句话说,如果字符串为空,则返回true,否则返回false。
从JDK 1.6开始,java 字符串包含String类的isEmpty()方法。
public boolean isEmpty() {
return value.length == 0;
}
字符串 isEmpty()方法的签名或语法如下:
public boolean isEmpty()
如果length为0,则为true,否则为false。
1.6
public class IsEmptyExample{
public static void main(String args[]){
String s1=;
String s2=javatpoint;
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
}}
true
false
public class IsEmptyExample2 {
public static void main(String[] args) {
String s1=;
String s2=Javatpoint;
// Either length is zero or isEmpty is true
if(s1.length()==0 || s1.isEmpty())
System.out.println(String s1 is empty);
else System.out.println(s1);
if(s2.length()==0 || s2.isEmpty())
System.out.println(String s2 is empty);
else System.out.println(s2);
}
}
String s1 is empty
Javatpoint