java 字符串空检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7528562/
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
String null check
提问by Pawan
I have tried this for 2 hours , but I've given up. I am checking the value of a String this way:
我已经尝试了 2 个小时,但我已经放弃了。我正在以这种方式检查 String 的值:
if(value.equals("")||value.length()<0||value==null)
{
value = "Anynomous"
}
But still the string is printing the value null
.
但字符串仍在打印 value null
。
Please see this program code here
请在此处查看此程序代码
<HTML>
<BODY>
<%!
String value ;
%>
<%
value = (String)request.getAttribute("retunval");
System.out.println("Inside New fgdf JSP");
if(value.equals("")||value.length()<0||value==null)
value = "Anonymuos";
%>
Hello <%=value%>
<BR>
</BR>
<BR>
</BR>
<a href="Test.jsp">Try Again</a>
</BODY>
</HTML>
采纳答案by Ankit Bansal
i think
我认为
value = (String)request.getAttribute("retunval");
value = (String)request.getAttribute("retunval");
is returning you a "null" string. When you try to do if tests that you have mentioned, it will return false because you are not checking
正在返回一个“空”字符串。当您尝试执行您提到的 if 测试时,它将返回 false,因为您没有检查
value.equals("null")
value.equals("null")
so that's why you get output as null.
所以这就是为什么你得到输出为空的原因。
回答by ig0774
Check whether value
is null
first. Otherwise value.equals()
will throw a NullPointerException.
检查是否value
是null
第一。否则value.equals()
将抛出 NullPointerException。
if (value == null || value.equals("")) {
value = "Anonymous";
}
回答by Thomas
First you need to reorder your if condition, otherwise you could run into NPEs. Additionally, the string length can never be lower than 0, so that check is not needed:
首先,您需要对 if 条件重新排序,否则可能会遇到 NPE。此外,字符串长度永远不能小于 0,因此不需要检查:
value==null||value.equals("")
Alternatively, if you can use Apache Commons Lang you could use StringUtils.isEmpty(value)
which handles all the relevant cases for you.
或者,如果您可以使用 Apache Commons Lang,您可以使用StringUtils.isEmpty(value)
它为您处理所有相关案例。
回答by Ed Staub
You need to add
你需要添加
||value.equals("null")
Somewhere something already did a toString() on the null value. Note the other answers as well - but this is the cause of the specific problem you're reporting.
某处已经对空值执行了 toString() 。还要注意其他答案 - 但这是您报告的特定问题的原因。
So you'd end up with, as one option:
所以你最终会得到,作为一种选择:
if ( value==null || value.isEmpty() || "null".equals(value) )
Because of the null-check, inverting the .equals() isn't necessary in this case - but it's a good habit to get into.
由于空检查,在这种情况下不需要反转 .equals() - 但这是一个好习惯。
回答by Sean Patrick Floyd
if(value.equals("")||value.length()<0||value==null)
value = "Anynomous"
The second and third condition can never evaluate to true!
(A String's length can never be less than zero, and if value was null, value.equals("")
would have caused a NullPointerException
)
第二个和第三个条件永远不能评估为真!(字符串的长度永远不能小于零,如果值为空,value.equals("")
则会导致NullPointerException
)
try this instead:
试试这个:
if(value==null||"".equals(value))
回答by Péter T?r?k
value.length()<0
will never be true - use value.length()==0
instead. However, in this case, the previous check (value.equals("")
) is equivalent to this one, so you can omit it. Apart from this, the condition should detect empty/null strings (except that the null check should be first, to avoid dereferencing a null pointer).
value.length()<0
永远不会是真的 -value.length()==0
改用。但是,在这种情况下,前面的检查 ( value.equals("")
) 与此等效,因此可以省略它。除此之外,条件应该检测空/空字符串(除了应该首先进行空检查,以避免取消引用空指针)。
Is it possible that value
contains the text "null"
?
是否可能value
包含文本"null"
?
回答by palacsint
Reorder as Thomas and ig0774 suggested or use the StringUtils.isEmpty()
method of Apache Commons Lang.
按照 Thomas 和 ig0774 的建议重新排序或使用Apache Commons Lang的StringUtils.isEmpty()
方法。
回答by Satya
Use this :
用这个 :
if(value == null || value.trim().length() <=0 || value.trim().equals("null")){
value ="Ananymous";
}
回答by Ganesh Varahade
I think this would be the best condition to check for it
我认为这将是检查它的最佳条件
if(value==null || value.trim().length()==0 || value.equals("null"))
if(value==null || value.trim().length()==0 || value.equals("null"))
it also eliminate fact if value=" " contains spaces.
如果 value="" 包含空格,它也会消除事实。
回答by Mike C
Is it possible that the value of the string is 'null'? (That is, not null, but a string which reads 'null')
字符串的值是否可能为“空”?(也就是说,不是空,而是一个读取“空”的字符串)