如何在java中检查字符串中的null?我在用
stringname.equalsignorecase(null)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
Dea*_*n J 155
string == null compares if the object is null. string.equals("foo") compares the value inside of that object. string == "foo" doesn't always work, because you're trying to see if the objects are the same, not the values they represent.
Longer answer:
If you try this, it won't work, as you've found:
String foo = null;
if (foo.equals(null)) {
// That fails every time.
}
Run Code Online (Sandbox Code Playgroud)
The reason is that foo is null, so it doesn't know what .equals is; there's no object there for .equals to be called from.
What you probably wanted was:
String foo = null;
if (foo == null) {
// That will work.
}
Run Code Online (Sandbox Code Playgroud)
The typical way to guard yourself against a null when dealing with Strings is:
String foo = null;
String bar = "Some string";
...
if (foo != null && foo.equals(bar)) {
// Do something here.
}
Run Code Online (Sandbox Code Playgroud)
That way, if foo was null, it doesn't evaluate the second half of the conditional, and things are all right.
The easy way, if you're using a String literal (instead of a variable), is:
String foo = null;
...
if ("some String".equals(foo)) {
// Do something here.
}
Run Code Online (Sandbox Code Playgroud)
If you want to work around that, Apache Commons has a class - StringUtils - that provides null-safe String operations.
if (StringUtils.equals(foo, bar)) {
// Do something here.
}
Run Code Online (Sandbox Code Playgroud)
Another response was joking, and said you should do this:
boolean isNull = false;
try {
stringname.equalsIgnoreCase(null);
} catch (NullPointerException npe) {
isNull = true;
}
Run Code Online (Sandbox Code Playgroud)
Please don't do that. You should only throw exceptions for errors that are exceptional; if you're expecting a null, you should check for it ahead of time, and not let it throw the exception.
在我看来,有两个原因.首先,例外情况很慢; 检查null很快,但是当JVM抛出异常时,需要花费很多时间.其次,如果您只是提前检查空指针,则代码更容易阅读和维护.
mik*_*iku 29
s == null
Run Code Online (Sandbox Code Playgroud)
不行吗?
aio*_*obe 17
当然有效.你错过了代码的重要部分.你只需要这样做:
boolean isNull = false;
try {
stringname.equalsIgnoreCase(null);
} catch (NullPointerException npe) {
isNull = true;
}
Run Code Online (Sandbox Code Playgroud)
;)
Kis*_*ela 10
使用TextUtils方法.
TextUtils.isEmpty(str):如果字符串为null或0-length,则返回true.参数:str要检查的字符串返回:如果str为null或零长度,则返回true
if(TextUtils.isEmpty(str)) {
// str is null or lenght is 0
}
Run Code Online (Sandbox Code Playgroud)
以下是此方法的源代码
/**
* Returns true if the string is null or 0-length.
* @param str the string to be examined
* @return true if str is null or zero length
*/
public static boolean isEmpty(CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
如果我们看一下equalsIgnoreCase方法的实现,我们会发现这个部分:
if (string == null || count != string.count) {
return false;
}
Run Code Online (Sandbox Code Playgroud)
因此,false如果参数是,它将始终返回null.这显然是正确的,因为它应该返回的唯一情况true是在a上调用equalsIgnoreCase null String,但是
String nullString = null;
nullString.equalsIgnoreCase(null);
Run Code Online (Sandbox Code Playgroud)
肯定会导致NullPointerException.
因此equals方法不是为了测试对象是否为null而设计的,只是因为你无法调用它们null.