是否有任何内置实用程序来检查 "null" 字符串而不是空值?
Is there any built in utility to check for "null" String instead of null?
我正在从 Apache Avro GenericRecord
中提取一个变量值,所以我的字符串变量值是 "null" 而不是 null。
GenericRecord payload = decoder.decode(record.value());
String userid = String.valueOf(payload.get("userId"));
// here userid is null string as "null"
System.out.println(Strings.isNullOrEmpty(userid));
并且因为那个 "null" 字符串,我的 sysout
打印出来是假的。我怎样才能检查它以便它打印出来 "true" bcoz string was a null string。是否有任何内置实用程序可以执行此操作,或者我必须编写自己的“.equals”检查?
"null"
是普通字符串类型,直接用String
类型的API即可:.equals("null")
即可。
您可能使用 String.valueOf(...)
方法自行生成 "null"
字符串。来自 String#valueOf
的 JavaDoc:
Returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
因此,我建议使用以下代码:
GenericRecord payload = decoder.decode(record.value());
boolean isNull = payload.get("userId") == null;
if (!isNull) {
String userid = payload.get("userId").toString();
}
这通过不生成它来防止与 "null"
字符串进行比较的问题。
我正在从 Apache Avro GenericRecord
中提取一个变量值,所以我的字符串变量值是 "null" 而不是 null。
GenericRecord payload = decoder.decode(record.value());
String userid = String.valueOf(payload.get("userId"));
// here userid is null string as "null"
System.out.println(Strings.isNullOrEmpty(userid));
并且因为那个 "null" 字符串,我的 sysout
打印出来是假的。我怎样才能检查它以便它打印出来 "true" bcoz string was a null string。是否有任何内置实用程序可以执行此操作,或者我必须编写自己的“.equals”检查?
"null"
是普通字符串类型,直接用String
类型的API即可:.equals("null")
即可。
您可能使用 String.valueOf(...)
方法自行生成 "null"
字符串。来自 String#valueOf
的 JavaDoc:
Returns: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
因此,我建议使用以下代码:
GenericRecord payload = decoder.decode(record.value());
boolean isNull = payload.get("userId") == null;
if (!isNull) {
String userid = payload.get("userId").toString();
}
这通过不生成它来防止与 "null"
字符串进行比较的问题。