在Java中,可以使用以下方法来判断一个字符串是否为空:
isEmpty()
方法:String str = "";
if (str.isEmpty()) {
System.out.println("字符串为空");
} else {
System.out.println("字符串不为空");
}
length()
方法:String str = "";
if (str.length() == 0) {
System.out.println("字符串为空");
} else {
System.out.println("字符串不为空");
}
equals()
方法:String str = "";
if (str.equals("")) {
System.out.println("字符串为空");
} else {
System.out.println("字符串不为空");
}
这些方法都可以用来判断一个字符串是否为空,可以根据实际需求选择其中的一种方法来使用。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:java怎么判断空字符串