gpt4 book ai didi

java - String equalsIgnoreCase() 与 JDK 7 Objects.equals(Object, Object)

转载 作者:行者123 更新时间:2023-12-04 16:36:15 26 4
gpt4 key购买 nike

通过替换以下内容,我是否会丢失任何东西:

    public final boolean testEquals(String left, String right) {
if ((left == null && right != null) || (left != null && right == null)) {
return false;
} else if (left == null && right == null) {
return true;
} else {
return left.equalsIgnoreCase(right);
}
}

来自 JDK 7 Objects class :
    public final boolean testEquals(String left, String right) {
return Objects.equals(left,right);
}

如果我失去了不区分大小写的功能,则将其替换为
Objects.equals(left.toLowerCase(),right.toLowerCase()); 

足够的?这样做有意义吗?

最佳答案

如果您不仅限于 JDK,您还可以使用

StringUtils.equalsIgnoreCase(CharSequence str1, CharSequence str2)

来自 apache-commons lang这是空安全的,将返回

 StringUtils.equalsIgnoreCase(null, null)   = true
StringUtils.equalsIgnoreCase(null, "abc") = false
StringUtils.equalsIgnoreCase("abc", null) = false
StringUtils.equalsIgnoreCase("abc", "abc") = true
StringUtils.equalsIgnoreCase("abc", "ABC") = true

关于java - String equalsIgnoreCase() 与 JDK 7 Objects.equals(Object, Object),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31833722/

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