length()是求String字符串对象中字符的个数;
length是求字符串数组中有多少个字符串;
public class strLen {
public static void main(String[] args) {
String str1 = "abcdefg";
String[] str2 = {"melon","apple","pear","banana"};
int s1,s2;
s1 = str1.length();
s2 = str2.length;
System.out.println(s1);
System.out.println(s2);
}
}
输出结果:
7
4
参考:https://www.cnblogs.com/dennisit/p/3705374.html
1.StringUtils.isEmpty(String str) 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0
System.out.println(StringUtils.isEmpty(null)); //true
System.out.println(StringUtils.isEmpty("")); //true
System.out.println(StringUtils.isEmpty(" ")); //false
System.out.println(StringUtils.isEmpty("dd")); //false
StringUtils.isNotEmpty(String str) 等价于 !isEmpty(String str)
2.StringUtils.isBlank(String str) 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成
ystem.out.println(StringUtils.isBlank(null)); //true
System.out.println(StringUtils.isBlank("")); //true
System.out.println(StringUtils.isBlank(" ")); //true
System.out.println(StringUtils.isBlank("dd")); //false
总结:isBlank中,对包含空格的字符串,也返回true。
3.其实isEmpty完全等同于string.length()==0
如果String本身是null,那么使用string.isEmpty()会报空指针异常(NullPointerException)
判断一个String为空的最安全的方法,还是 string ==null || string.isEmpty()
总结:StringUtils.isEmpty(String str) 使用它更靠谱一些。