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

首先,来看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()是一样的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值