Check whether a string is not null and not empty

后端 未结 30 2429
予麋鹿
予麋鹿 2020-11-22 02:13

How can I check whether a string is not null and not empty?

public void doStuff(String str)
{
    if (str != null && str != \"**here I want to check          


        
30条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 03:09

    I've encountered a situation where I must check that "null" (as a string) must be regarded as empty. Also white space and an actual null must return true. I've finally settled on the following function...

    public boolean isEmpty(String testString) {
      return ((null==testString) || "".equals((""+testString).trim()) || "null".equals((""+testString).toLowerCase()));
    }
    

提交回复
热议问题