java.lang.String.isEmpty()的用法

来源:互联网 发布:windows截屏快捷键alt 编辑:程序博客网 时间:2024/05/22 08:38

首先,来看isEmpty()的定义:

Declaration

Following is the declaration for java.lang.String.isEmpty() method

public boolean isEmpty()

Parameters

  • NA

Return Value

This method returns true if length() is 0, else false.

Exception

  • NA

也就是说,isEmpty()方法只负责判断字符串的长度是否为0,也就是针对""的情况。如果字符串为Null,其返回为false。


1.在实际情况中,常有条件:

if(!str.isEmpty()){

//如果字符串长度>0,则...

}

但是,在无法判断字符串是否为null的时候,应当完善上述条件:

if(str!=null && !str.isEmpty()){

//如果字符串长度>0,则...
}


2.对于"".equals(str),则equals will check its argument and return false if it's null。用法和isEmpty()是一样的。

0 0
原创粉丝点击