检查条件是否为“null”

我对检查null条件有疑问。例如:

if(some conditon) value1= value; //value1 is string type else value1= ""; 

类似地,一些其他4个字符串值具有类似的条 我需要的是我想检查所有这5个字符串值是否为null,为了做其他特定部分。 我是这样做的

 if(value1 == null) { } 

但是pgm控件没有进入循环事件,而值为1 =“”。 然后我试过了

 if(value1 ==""){ } 

这也没有用。

我们不能检查null和“”值是否相同? 谁能帮我??

如果要检查String是否为null,则使用

 if (s == null) 

如果要检查字符串是否为空字符串,请使用

 if (s.equals("")) 

要么

 if (s.length() == 0) 

要么

 if (s.isEmpty()) 

空字符串是空字符串。 它不是空的。 并且==绝不能用于比较字符串内容。 ==测试两个变量是否引用同一个对象实例。 如果它们包含相同的字符则不会。

要检查String上的“is not null”和“is not empty”,请使用static

 TextUtils.isEmpty(stringVariableToTest) 

看起来你想检查String是否为空。

 if (string.isEmpty()) 

您无法通过执行if (string == "")来检查它,因为您正在比较String对象。 它们永远不会相同,因为你有两个不同的对象。 要比较字符串,请使用string.equals()

当你在使用String时总是使用.equals

equals()函数是Object类的一个方法,应该被程序员覆盖。

如果你想检查字符串是否为null,那么if (string.isEmpty())否则你也可以尝试if (string.equals(null))

您可以使用:

我们可以通过两种方式检查字符串是否为空:

  • if(s != null && s.length() == 0)
  • if(("").equals(s))

喜欢下面。

 String str; if(str.length() > 0) { Log.d("log","str is not empty"); } else { Log.d("log","str is empty"); }