gpt4 book ai didi

java - 检查空字符串 null 吗?

转载 作者:行者123 更新时间:2023-12-02 09:15:06 24 4
gpt4 key购买 nike

我有一个 json,其中 userId 属性作为字符串“null” -

"userId":"null"

我有一个方法可以检查我的字符串是否为空 -

public static boolean isEmpty(String value) {
return value == null || value.isEmpty();
}

但是每次我的上述方法都会将上面的 userId 返回为 false ?它应该为 true,因为 userId 为 null。 Guava 或 Apache Commons 中是否有其他 api 可以做到这一点?

最佳答案

null 不等于字符串 "null"null 表示给定对象尚未赋值,其中字符串 "null" 是有效对象。它包含字符 n u l l,这是字符串的有效值。您还需要检查该值是否为文字字符串 "null" 才能执行您想要的操作。

检查正确

return value == null || value.isEmpty() || value.equals("null") ;

如果您仍想将 "null" 保留为有效用户名,请将发送 json 的内容更改为以下格式,该格式应解释为文字 null 而不是内容为 "null"

的字符串
"userId":null

关于java - 检查空字符串 null 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29499693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com