Java 为空时获取空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21936503/
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
Get empty string when null
提问by Spring
I want to get string values of my fields (they can be type of long string or any object),
我想获取我的字段的字符串值(它们可以是长字符串或任何对象的类型),
if a field is null then it should return empty string, I did this with guava;
如果一个字段为空,那么它应该返回空字符串,我用番石榴做到了这一点;
nullToEmpty(String.valueOf(gearBox))
nullToEmpty(String.valueOf(id))
...
But this returns null if gearbox is null! Not empty string because valueOf methdod returns string "null" which leads to errors.
但是如果变速箱为空,这将返回空值!不是空字符串,因为 valueOf 方法返回导致错误的字符串“null”。
Any Ideas?
有任何想法吗?
EDIt: there are 100s fields I look for something easy to implement
编辑:有 100 个字段我在寻找易于实现的东西
采纳答案by arshajii
You can use Objects.toString()
(standard in Java 7):
您可以使用Objects.toString()
(Java 7 中的标准):
Objects.toString(gearBox, "")
Objects.toString(id, "")
From the linked documentation:
从链接的文档:
public static String toString(Object o, String nullDefault)
Returns the result of calling
toString
on the first argument if the first argument is not null and returns the second argument otherwise.Parameters:
o
- an objectnullDefault
- string to return if the first argument isnull
Returns:
the result of callingtoString
on the first argument if it is notnull
and the second argument otherwise.See Also:
toString(Object)
public static String toString(Object o, String nullDefault)
toString
如果第一个参数不为空,则返回调用第一个参数的结果,否则返回第二个参数。参数:
o
- 一个对象nullDefault
- 如果第一个参数是要返回的字符串null
返回:如果不是,则
调用toString
第一个参数的结果,null
否则调用第二个参数。也可以看看:
toString(Object)
回答by Samhain
Use an inline null check
使用内联空检查
gearBox == null ? "" : String.valueOf(gearBox);
回答by Keppil
If you don't mind using Apache commons, they have a StringUtils.defaultString(String str)
that does this.
如果您不介意使用 Apache 公共资源,他们有一个StringUtils.defaultString(String str)
可以做到这一点。
Returns either the passed in String, or if the String is null, an empty String ("").
返回传入的字符串,或者如果字符串为空,则返回一个空字符串 ("")。
If you also want to get rid of "null"
, you can do:
如果你也想摆脱"null"
,你可以这样做:
StringUtils.defaultString(str).replaceAll("^null$", "")
or to ignore case:
或忽略大小写:
StringUtils.defaultString(str).replaceAll("^(?i)null$", "")
回答by renke
Since you're using guava:
由于您使用的是番石榴:
Objects.firstNonNull(gearBox, "").toString();
回答by Federico Piazza
For java 8 you can use Optional approach:
对于 java 8,您可以使用 Optional 方法:
Optional.ofNullable(gearBox).orElse("");
Optional.ofNullable(id).orElse("");
回答by Won-Sik Kim
If alternative way, Guavaprovides Strings.nullToEmpty(String)
.
如果替代方式,Guava提供Strings.nullToEmpty(String)
.
Source code
源代码
String str = null;
str = Strings.nullToEmpty(str);
System.out.println("String length : " + str.length());
Result
结果
0
回答by delive
The best solution for all the versions is this clear example:
所有版本的最佳解决方案是这个清晰的例子:
Implementation method
实施方法
// object to Object string
public static Object str(Object value) {
if (value == null) {
//value = new String();
value = "";
}
return value;
}
// Object to String
public static String str(Object value) {
if (value == null) {
//value = new String();
value = "";
}
return value.toString();
}
// String to String (without nulls)
public String str(String value) {
if (value == null) {
//value = new String();
value = "";
}
return value;
}
Use:
用:
str(yourString);