检查字符串在Java中是否为空格,空(“”)或null
假设以下是我们的字符串。
String myStr = "";
现在,我们将检查上面的字符串是空格,空(“”)还是null。
if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) {
System.out.println("String is not null or not empty or not whitespace");
} else {
System.out.println("String is null or empty or whitespace");
}以下是检查空字符串的示例。
示例
public class Demo {
public static void main(String[] args) {
String myStr = "";
if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) {
System.out.println("String is not null or not empty or not whitespace");
} else {
System.out.println("String is null or empty or whitespace");
}
}
}输出结果
String is null or empty or whitespace
让我们看另一个检查空白输入的示例。
示例
public class Demo {
public static void main(String[] args) {
String myStr = " ";
if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) {
System.out.println("String is not null or not empty or not whitespace");
} else {
System.out.println("String is null or empty or whitespace");
}
}
}输出结果
String is null or empty or whitespace
热门推荐
10 新婚过年祝福语大全简短
11 春天变冷祝福语简短
12 离职横幅升职祝福语简短
13 订婚新发言简短祝福语
14 恭喜朋友买车祝福语简短
15 送弟弟中秋祝福语简短
16 六一婚礼文案祝福语简短
17 虎年送火腿祝福语简短
18 同事辞职正常祝福语简短