如何比较Java中的字符串?
到目前为止,我一直在程序中使用如何比较Java中的字符串?,java,string,equality,Java,String,Equality,到目前为止,我一直在程序中使用==操作符来比较所有字符串。 但是,我遇到了一个错误,将其中一个错误改为.equals(),它修复了这个错误 ==坏吗?什么时候应该使用,什么时候不应该使用?有什么区别吗?=测试引用是否相等(它们是否是相同的对象) .equals()测试值是否相等(它们在逻辑上是否“相等”) 在调用.equals()之前检查null,这样您就不必这样做了(从JDK7开始提供,也在中提供) 因此,如果要测试两个字符串是否具有相同的值,可能需要使用Objects.equals() 您几
==
操作符来比较所有字符串。
但是,我遇到了一个错误,将其中一个错误改为.equals()
,它修复了这个错误
==
坏吗?什么时候应该使用,什么时候不应该使用?有什么区别吗?=
测试引用是否相等(它们是否是相同的对象)
.equals()
测试值是否相等(它们在逻辑上是否“相等”)
在调用.equals()
之前检查null
,这样您就不必这样做了(从JDK7开始提供,也在中提供)
因此,如果要测试两个字符串是否具有相同的值,可能需要使用Objects.equals()
您几乎总是想使用对象.equals()
。在罕见的情况下,如果知道要处理字符串,可以使用==
发件人:
此外,字符串文本总是引用类string
的同一实例。这是因为使用方法string.intern
,字符串文本(或者更一般地说,常量表达式()的值字符串)被“interned”以共享唯一实例
类似的例子也可以在中找到
其他考虑方法
忽略大小写的值相等。但是,请注意,在各种与区域设置相关的情况下,此方法可能会产生意外的结果,请参阅
将
字符串
的内容与任何字符序列
的内容进行比较(自Java 1.5起提供)。不必在进行相等比较之前将StringBuffer等转换为字符串,但将空检查留给您。是的,==
不利于比较字符串(任何对象都是真实的,除非您知道它们是规范的)<代码>=只是比较对象引用.equals()
测试是否相等。对于字符串,它们通常是相同的,但正如您所发现的,这并不总是可以保证的。==
比较Java中的对象引用,而字符串
对象也不例外
要比较对象的实际内容(包括字符串
),必须使用等于
方法
如果使用==
对两个String
对象进行比较,结果是true
,这是因为String
对象被插入,并且Java虚拟机有多个引用指向同一个String
实例。我们不应该期望使用=
将一个字符串
对象与另一个字符串
对象进行内容相同的比较以评估为真
是的,这是不好的
=
表示两个字符串引用是完全相同的对象。您可能听说过这种情况,因为Java保留了某种文字表(确实如此),但情况并非总是如此。有些字符串是以不同的方式加载的,由其他字符串等构造而成,因此决不能假设两个相同的字符串存储在同一位置
Equals为您进行真正的比较 .equals()
比较类中的数据(假设函数已实现)。
=
比较指针位置(对象在内存中的位置)
=
如果两个对象(不谈论原语)指向同一个对象实例,则返回true。
.equals()
如果两个对象包含相同的数据,则返回true
这可能会对您有所帮助。操作员检查两个字符串是否完全相同
.equals()
方法将检查两个字符串是否具有相同的值
String a = new String("foo");
String b = new String("foo");
System.out.println(a == b); // prints false
System.out.println(a.equals(b)); // prints true
确保你明白为什么。这是因为==
比较只比较引用;equals()
方法对内容进行逐字符比较
当您为
a
和b
调用new时,每一个都会得到一个新的引用,该引用指向字符串表中的“foo”
。引用不同,但内容相同。==
测试对象引用,.equals()
测试字符串值
有时它看起来好像比较了值,因为Java做了一些幕后工作来确保相同的内联字符串实际上是相同的对象
例如:
String fooString1 = new String("foo");
String fooString2 = new String("foo");
// Evaluates to false
fooString1 == fooString2;
// Evaluates to true
fooString1.equals(fooString2);
// Evaluates to true, because Java uses the same object
"bar" == "bar";
但是要小心空值强>
=
可以处理null
字符串,但是从null字符串调用.equals()
将导致异常:
String nullString1 = null;
String nullString2 = null;
// Evaluates to true
System.out.print(nullString1 == nullString2);
// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));
因此,如果您知道fooString1
可能为空,请通过编写
System.out.print(fooString1 != null && fooString1.equals("bar"));
以下内容较短,但不太明显的是它会检查null:
System.out.print("bar".equals(fooString1)); // "bar" is never null
System.out.print(Objects.equals(fooString1, "bar")); // Java 7 required
我同意zacherates的回答 但您可以对非文本字符串调用
intern()
来自zacherates的例子:
// ... but they are not the same object
new String("test") == "test" ==> false
String string1 = "foo";
String string2 = "foo";
// test for equality with the java string equals method
if (string1.equals(string2))
{
// this line WILL print
System.out.println("The two strings are the same.")
}
如果您实习,则非文字字符串相等为true
:
new String("test").intern() == "test" ==> true
Java中的字符串是不可变的。这意味着无论何时尝试更改/修改字符串,都会得到一个新实例。不能更改原始字符串。这样做是为了缓存这些字符串实例。一个典型的程序包含很多字符串引用,缓存这些实例可以减少内存占用并提高程序的性能 当使用==运算符进行字符串比较时,您不是在比较字符串的内容,而是在比较内存地址。如果两者相等,则返回true,否则返回false。而字符串中的equals比较字符串内容 所以问题是如果所有的字符串都缓存在系统中,为什么
==
String a="Test"; String b="Test"; if(a==b) ===> true
String Pool
b -----------------> "test" <-----------------a
String a="test";
String b=new String("test");
if (a==b) ===> false
String Pool
"test" <-------------------- a
Heap
"test" <-------------------- b
String a="Test";
String b="Test";
if(a.equals(b)) ===> true
String a="test";
String b=new String("test");
if(a.equals(b)) ===> true
public float simpleSimilarity(String u, String v) {
String[] a = u.split(" ");
String[] b = v.split(" ");
long correct = 0;
int minLen = Math.min(a.length, b.length);
for (int i = 0; i < minLen; i++) {
String aa = a[i];
String bb = b[i];
int minWordLength = Math.min(aa.length(), bb.length());
for (int j = 0; j < minWordLength; j++) {
if (aa.charAt(j) == bb.charAt(j)) {
correct++;
}
}
}
return (float) (((double) correct) / Math.max(u.length(), v.length()));
}
String a = "This is the first string.";
String b = "this is not 1st string!";
// for exact string comparison, use .equals
boolean exact = a.equals(b);
// For similarity check, there are libraries for this
// Here I'll try a simple example I wrote
float similarity = simple_similarity(a,b);
Example:
stringObjectOne.equals(stringObjectTwo);
String one = "HELLO";
String two = "HELLO";
String three = new String("HELLO");
String four = "hello";
one == two; // TRUE
one == three; // FALSE
one == four; // FALSE
one.equals(two); // TRUE
one.equals(three); // TRUE
one.equals(four); // FALSE
one.equalsIgnoreCase(four); // TRUE
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2); // It prints false (reference comparison)
System.out.println(s1.equals(s2)); // It prints true (content comparison)
if (string1.equals(string2))
String string1 = "foo";
String string2 = "FOO";
if (string1.equals(string2))
{
// this line will not print because the
// java string equals method returns false:
System.out.println("The two strings are the same.")
}
String string1 = "foo";
String string2 = "foo";
// test for equality with the java string equals method
if (string1.equals(string2))
{
// this line WILL print
System.out.println("The two strings are the same.")
}
String string1 = "foo";
String string2 = "FOO";
// java string compare while ignoring case
if (string1.equalsIgnoreCase(string2))
{
// this line WILL print
System.out.println("Ignoring case, the two strings are the same.")
}
String string1 = "foo bar";
String string2 = "foo bar";
// java string compare example
if (string1.compareTo(string2) == 0)
{
// this line WILL print
System.out.println("The two strings are the same.")
}
String s1 = "Stack Overflow";
String s2 = "Stack Overflow";
s1 == s2; //true
s1.equals(s2); //true
String s1 = new String("Stack Overflow");
String s2 = new String("Stack Overflow");
s1 == s2; //false
s1.equals(s2); //true
str1 = new String("abc");
str2 = new String("abc");
System.out.println((str1.equals(str2))?"TRUE":"FALSE");
System.out.println((str1==str2) ? "TRUE" : "FALSE");