isEmpty()
方法判断字符串是否为空,例如:String str = "";
if(str.isEmpty()) {
System.out.println("字符串为空");
}
StringUtils
工具类中的isBlank()
方法判断字符串是否为空或只包含空白字符,例如:import org.apache.commons.lang3.StringUtils;
String str = " ";
if(StringUtils.isBlank(str)) {
System.out.println("字符串为空或只包含空白字符");
}
trim()
方法去除字符串两端的空白字符,然后再判断是否为空,例如:String str = " ";
if(str.trim().isEmpty()) {
System.out.println("字符串为空");
}
String str = " ";
str = str.replaceAll("\\s", "");
if(str.isEmpty()) {
System.out.println("字符串为空");
}
Objects
工具类中的isNull()
方法判断字符串是否为null,例如:import java.util.Objects;
String str = null;
if(Objects.isNull(str)) {
System.out.println("字符串为空");
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:处理c语言空字符串有哪些技巧