[英]StringUtils.isBlank return false for null Strings
I am working in a Java project and my code shows weird behavior. 我在Java项目中工作,我的代码显示奇怪的行为。 Here is my code:
这是我的代码:
String access = String.valueOf(getStringvalue());
Boolean isBlank = StringUtils.isBlank(access);
In the above code, 'access' object can have null values. 在上面的代码中,'access'对象可以具有空值。 And it is said that if we pass a null value to
StringUtils.isBlank()
, it will return true. 据说如果我们将一个空值传递给
StringUtils.isBlank()
,它将返回true。 But here I returned only false value when access
is null. 但是,当
access
为空时,我只返回false值。 What is the reason for this behavior? 这种行为的原因是什么?
I also had this problem and found the trick just after saw the source code of String.valueof(). 我也遇到了这个问题,并在看到String.valueof()的源代码之后发现了这个技巧。 The source code of String.valueof() is below.
String.valueof()的源代码如下。
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
Therefore when you pass a null object, you will get a 'null' String. 因此,当您传递一个null对象时,您将获得一个'null'字符串。 Therefore your StringUtils.isBlank() will treat is as a String rather than a null object and you will get a 'false' booean value.
因此,您的StringUtils.isBlank()将视为String而不是null对象,您将获得'false'booean值。
Hope it helps.! 希望能帮助到你。!
I tested those code and they give the same result. 我测试了这些代码并给出了相同的结果。
Boolean isBlank = StringUtils.isBlank("");
System.out.println(isBlank);// give true
and 和
Boolean isBlank = StringUtils.isBlank(null);
System.out.println(isBlank);// give true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.