检查是否条件“空”(Checking if condition for 'null'

2019-08-16 21:05发布

我有一个关于检查空condition.For如一个疑问:

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

类似地一些其他4字符串有类似状况。 我需要的是我要检查所有的5字符串值是否为空或不是,中序做一些其他的特定部分。 我做了这样的

if(value1 == null)
{
}

但PGM控制因此未进入环eventhough值1 =“”。 然后我试着

if(value1 ==""){
} 

这也didnt工作。

我们不能检查零和“”值相同? 谁能帮我??

Answer 1:

如果您想检查是一个字符串为空,可以使用

if (s == null)

如果你想检查一个字符串是空字符串,您使用

if (s.equals(""))

要么

if (s.length() == 0)

要么

if (s.isEmpty())

空字符串为空字符串。 它不是空。 而==绝不能用来比较字符串内容。 ==测试,如果两个变量参考,以同一个对象实例。 若它们包含相同字符。



Answer 2:

同时检查“不为空”和一根绳子“不空”,使用静态

TextUtils.isEmpty(stringVariableToTest)


Answer 3:

它看起来像你想请检查是否字符串为空。

if (string.isEmpty())

你不能确认这样做if (string == "")因为你是比较String对象。 他们从来不一样的,因为你有两个不同的对象。 比较字符串,使用string.equals()



Answer 4:

当您在字符串工作始终使用.equals

等于()函数是应当由程序员重写对象类的方法。

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



Answer 5:

您可以使用:

我们可以检查一个字符串是否在两个方面空:

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


Answer 6:

下面喜欢。

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


文章来源: Checking if condition for 'null'