检查空字符串 null?
Check for empty string null?
我有一个 json,其中 userId
属性 作为字符串 "null" -
"userId":"null"
我有一个方法可以检查我的字符串是否为空 -
public static boolean isEmpty(String value) {
return value == null || value.isEmpty();
}
但每次我的上述方法 returns 都支持我 false
上面 userId
?它应该是真的,因为 userId 是空的。 Guava 或 Apache Commons 中是否还有其他 api 可以做到这一点?
"null" 与 null 不同。
"null" 是单词 "null".
长度为 4 个字符的字符串
null(无引号)就是什么都没有。
值 null
不等于字符串 "null"
。 null
表示给定的对象还没有被赋值,其中String "null"
是一个有效的对象。它包含字符 n
u
l
l
,这是字符串的有效值。您还需要检查该值是否是文字字符串 "null"
才能执行您想要的操作。
正确检查
return value == null || value.isEmpty() || value.equals("null") ;
如果您仍想将 "null"
保留为有效用户名,则将发送 json 的任何内容更改为以下格式,应将其解释为文字 null
而不是比内容为 "null"
的字符串
"userId":null
{"userId":"null"}
等于 String userId = "null"
在 java.
{}
在解组时等于 String userId = null
。
我有一个 json,其中 userId
属性 作为字符串 "null" -
"userId":"null"
我有一个方法可以检查我的字符串是否为空 -
public static boolean isEmpty(String value) {
return value == null || value.isEmpty();
}
但每次我的上述方法 returns 都支持我 false
上面 userId
?它应该是真的,因为 userId 是空的。 Guava 或 Apache Commons 中是否还有其他 api 可以做到这一点?
"null" 与 null 不同。
"null" 是单词 "null".
长度为 4 个字符的字符串null(无引号)就是什么都没有。
值 null
不等于字符串 "null"
。 null
表示给定的对象还没有被赋值,其中String "null"
是一个有效的对象。它包含字符 n
u
l
l
,这是字符串的有效值。您还需要检查该值是否是文字字符串 "null"
才能执行您想要的操作。
正确检查
return value == null || value.isEmpty() || value.equals("null") ;
如果您仍想将 "null"
保留为有效用户名,则将发送 json 的任何内容更改为以下格式,应将其解释为文字 null
而不是比内容为 "null"
"userId":null
{"userId":"null"}
等于 String userId = "null"
在 java.
{}
在解组时等于 String userId = null
。