Java 如何检查我的字符串是否等于空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2601978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if my string is equal to null?
提问by Roman
I want to perform some action ONLY IF my string has a meaningful value. So, I tried this.
仅当我的字符串具有有意义的值时,我才想执行某些操作。所以,我试过这个。
if (!myString.equals("")) {
doSomething
}
and this
和这个
if (!myString.equals(null)) {
doSomething
}
and this
和这个
if ( (!myString.equals("")) && (!myString.equals(null))) {
doSomething
}
and this
和这个
if ( (!myString.equals("")) && (myString!=null)) {
doSomething
}
and this
和这个
if ( myString.length()>0) {
doSomething
}
And in all cases my program doSomething
in spite on the fact that my string IS EMPTY. It equals to null
. So, what is wrong with that?
在所有情况下,我的程序doSomething
尽管我的字符串是空的。它等于null
。那么,这有什么问题呢?
ADDED:
添加:
I found the reason of the problem. The variable was declared as a string and, as a consequence, null
assigned to this variable was transformed to "null"
! So, if (!myString.equals("null"))
works.
我找到了问题的原因。该变量被声明为字符串,因此,null
分配给该变量的变量被转换为"null"
! 所以,if (!myString.equals("null"))
工作。
回答by polygenelubricants
if (myString != null && !myString.isEmpty()) {
// doSomething
}
As further comment, you should be aware of this term in the equals
contract:
作为进一步的评论,您应该知道equals
合同中的这个条款:
From Object.equals(Object)
:
For any non-null reference value
x
,x.equals(null)
shouldreturn false
.
对于任何非空引用值
x
,x.equals(null)
应该return false
。
The way to compare with null
is to use x == null
and x != null
.
比较的方法null
是使用x == null
and x != null
。
Moreover, x.field
and x.method()
throws NullPointerException
if x == null
.
此外,x.field
并x.method()
抛出NullPointerException
if x == null
。
回答by WhirlWind
You need to check that the myString
object is null
:
您需要检查myString
对象是否为null
:
if (myString != null) {
doSomething
}
回答by bragboy
Try,
尝试,
myString!=null && myString.length()>0
回答by Michael Myers
If myString
is null
, then calling myString.equals(null)
or myString.equals("")
will fail with a NullPointerException
. You cannot call any instance methods on a null variable.
如果myString
是null
,则调用myString.equals(null)
ormyString.equals("")
将失败并显示NullPointerException
。您不能对空变量调用任何实例方法。
Check for null first like this:
首先像这样检查空值:
if (myString != null && !myString.equals("")) {
//do something
}
This makes use of short-circuit evaluationto not attempt the .equals
if myString
fails the null check.
这利用短路评估来不尝试.equals
ifmyString
使空检查失败。
回答by Andy White
If your string is null, calls like this should throw a NullReferenceException:
如果你的字符串为空,这样的调用应该抛出一个 NullReferenceException:
myString.equals(null)
myString.equals(null)
But anyway, I think a method like this is what you want:
但无论如何,我认为这样的方法就是你想要的:
public static class StringUtils
{
public static bool isNullOrEmpty(String myString)
{
return myString == null || "".equals(myString);
}
}
Then in your code, you can do things like this:
然后在您的代码中,您可以执行以下操作:
if (!StringUtils.isNullOrEmpty(myString))
{
doSomething();
}
回答by elduff
This should work:
这应该有效:
if (myString != null && !myString.equals(""))
doSomething
}
If not, then myString likely has a value that you are not expecting. Try printing it out like this:
如果不是,则 myString 可能具有您不期望的值。尝试像这样打印出来:
System.out.println("+" + myString + "+");
Using the '+' symbols to surround the string will show you if there is extra whitespace in there that you're not accounting for.
使用“+”符号包围字符串会告诉你那里是否有你没有考虑的额外空格。
回答by Richard H
if (myString != null && myString.length() > 0) {
// your magic here
}
Incidently, if you are doing much string manipulation, there's a great Spring class with all sorts of useful methods:
顺便说一句,如果您要进行大量字符串操作,那么有一个很棒的 Spring 类,其中包含各种有用的方法:
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/util/StringUtils.html
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/util/StringUtils.html
回答by James P.
I'd do something like this:
我会做这样的事情:
( myString != null && myString.length() > 0 )
? doSomething() : System.out.println("Non valid String");
- Testing for null checks whether myString contains an instance of String.
- length() returns the length and is equivalent to equals("").
- Checking if myString is null first will avoid a NullPointerException.
- 测试 null 检查 myString 是否包含 String 的实例。
- length() 返回长度,等效于 equals("")。
- 首先检查 myString 是否为 null 将避免 NullPointerException。
回答by Moe
if(str.isEmpty() || str==null){
do whatever you want
}
if(str.isEmpty() || str==null){
do whatever you want
}
回答by Matt
I would encourage using an existing utility, or creating your own method:
我鼓励使用现有的实用程序,或创建自己的方法:
public static boolean isEmpty(String string) {
return string == null || string.length() == 0;
}
Then just use it when you need it:
然后在需要时使用它:
if (! StringUtils.isEmpty(string)) {
// do something
}
As noted above, the || and && operators short circuit. That means as soon as they can determine their value they stop. So if (string == null) is true, the length part does not need to be evaluated, as the expression would always be true. Likewise with &&, where if the left side is false, the expression is always false and need not be evaluated further.
如上所述, || 和 && 运算符短路。这意味着一旦他们可以确定自己的价值,他们就会停止。因此,如果 (string == null) 为真,则不需要计算长度部分,因为表达式始终为真。与 && 类似,如果左侧为假,则表达式始终为假,无需进一步求值。
As an additional note, using length is generally a better idea than using .equals. The performance is slightly better (not much), and doesn't require object creation (though most compilers might optimize this out).
作为附加说明,使用长度通常比使用 .equals 更好。性能稍微好一些(不多),并且不需要创建对象(尽管大多数编译器可能会对此进行优化)。