isEmpty
: 如果字串长度为0,则返回true,否则返回false。
isBlank
: 如果字串为长度为0或只包含空格,则返回true,否则返回false。
例如使用此程式码:
String str1 = "";String str2 = " ";// 使用 isEmpty() 检查字符串是否为空if(str1.isEmpty()){ System.out.println("str1 is empty");}// 使用 isBlank() 检查字符串是否为空或只包含空格if(str2.isBlank()){ System.out.println("str2 is blank");}
则输出:
str1 is emptystr2 is blank
(新手上路,如有错误请友善告知,谢谢)