如何比较 Java 中的字符串? [英] How do I compare strings in Java?

查看:33
本文介绍了如何比较 Java 中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在我的程序中使用 == 运算符来比较我的所有字符串.但是,我遇到了一个错误,将其中一个改为 .equals() ,并且修复了该错误.

I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.

== 不好吗?什么时候应该使用,什么时候不应该使用?有什么区别?

Is == bad? When should it and should it not be used? What's the difference?

推荐答案

== 测试引用相等性(它们是否是同一个对象).

== tests for reference equality (whether they are the same object).

.equals() 测试值相等(它们在逻辑上是否相等").

.equals() tests for value equality (whether they are logically "equal").

Objects.equals() 在调用 .equals() 之前检查 null 所以你不必(从 JDK7 开始可用,也可在 Guava 中找到.

Objects.equals() checks for null before calling .equals() so you don't have to (available as of JDK7, also available in Guava).

因此,如果您想测试两个字符串是否具有相同的值,您可能需要使用 Objects.equals().

Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.equals().

// These two have the same value
new String("test").equals("test") // --> true 

// ... but they are not the same object
new String("test") == "test" // --> false 

// ... neither are these
new String("test") == new String("test") // --> false 

// ... but these are because literals are interned by 
// the compiler and thus refer to the same object
"test" == "test" // --> true 

// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true

// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true

您几乎总是想要使用Objects.equals().在您知道罕见情况下,您正在处理interned 字符串,你可以使用==.

You almost always want to use Objects.equals(). In the rare situation where you know you're dealing with interned strings, you can use ==.

来自 JLS 3.10.5.字符串文字:

此外,字符串字面量总是引用类 Stringsame 实例.这是因为字符串文字 - 或者,更一般地说,是作为常量表达式值的字符串 (§15.28) - 被拘留";以便共享唯一的实例,使用方法 String.intern.

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

类似的例子也可以在 JLS 中找到3.10.5-1.

Similar examples can also be found in JLS 3.10.5-1.

String.equalsIgnoreCase() 忽略大小写的值相等.但是请注意,此方法在各种与语言环境相关的情况下可能会产生意外结果,请参阅这个问题.

String.equalsIgnoreCase() value equality that ignores case. Beware, however, that this method can have unexpected results in various locale-related cases, see this question.

字符串.contentEquals()String 的内容与任何 CharSequence(自 Java 1.5 起可用)的内容进行比较.使您不必在进行相等比较之前将 StringBuffer 等转换为 String,但将空值检查留给您.

String.contentEquals() compares the content of the String with the content of any CharSequence (available since Java 1.5). Saves you from having to turn your StringBuffer, etc into a String before doing the equality comparison, but leaves the null checking to you.

这篇关于如何比较 Java 中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆