我想得到字段的字符串值(它们可以是长字符串或任何对象的类型) ,
如果一个字段为空,那么它应该返回空字符串,我用番石榴这样做;
nullToEmpty(String.valueOf(gearBox)) nullToEmpty(String.valueOf(id)) ...
但是如果变速箱为空,则返回空!不是空字符串,因为 valueOf method 返回字符串“ null”,这会导致错误。
有什么想法吗?
编辑: 有100个字段,我寻找一些容易实现的东西
Use an inline null check
gearBox == null ? "" : String.valueOf(gearBox);
You can use Objects.toString() (standard in Java 7):
Objects.toString()
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 object nullDefault - string to return if the first argument is null Returns: the result of calling toString on the first argument if it is not null and the second argument otherwise. See Also: toString(Object)
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 object nullDefault - string to return if the first argument is null Returns: the result of calling toString on the first argument if it is not null and the second argument otherwise. See Also: toString(Object)
Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.
toString
Parameters: o - an object nullDefault - string to return if the first argument is null
o
nullDefault
null
Returns: the result of calling toString on the first argument if it is not null and the second argument otherwise.
See Also: toString(Object)
toString(Object)
If you don't mind using Apache commons, they have a StringUtils.defaultString(String str) that does this.
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$", "")
Since you're using guava:
Objects.firstNonNull(gearBox, "").toString();
For java 8 you can use Optional approach:
Optional.ofNullable(gearBox).orElse(""); Optional.ofNullable(id).orElse("");
If alternative way, Guava provides Strings.nullToEmpty(String).
Strings.nullToEmpty(String)
Source code
String str = null; str = Strings.nullToEmpty(str); System.out.println("String length : " + str.length());
Result
0
In Java 9+ use : Objects.requireNonNullElse (obj, defaultObj) https://docs.oracle.com/javase/9/docs/api/java/util/Objects.html#requireNonNullElse-T-T-
//-- returns empty string if obj is null Objects.requireNonNullElse (obj, "")
StringUtils.defaultString(String str) Returns either the passed in String, or if the String is null, an empty String ("").
Example from java doc
StringUtils.defaultString(null) will return "" StringUtils.defaultString("") will return "" StringUtils.defaultString("bat") will return "bat"
StringUtils.defaultString(null)
""
StringUtils.defaultString("")
StringUtils.defaultString("bat")
"bat"