在本文中,您将学习如何比较字符串以及使用equals to (=)运算符比较字符串时出现的问题 。
介绍 该 String 是Java中的特殊类。我们在Java程序中定期使用String,因此比较两个字符串是Java中的一种常见做法。在本文中,我试图回答有关字符串的最常见问题,例如:“如何比较Java中的字符串?”
在身份验证,排序,引用匹配等过程中,比较字符串非常有帮助。
我列出了三种不同的比较Java字符串的方法。
使用 equals()方法(比较内容)
equals()
使用 ==运算符(比较对象引用)
==
使用 compareTo() 方法(按字典顺序比较字符串)
compareTo()
1.使用Equals() 方法比较字符串 这样,我正在使用 .equals() String类的实例方法。.equals()方法原本 是 Object 类方法,而String类会覆盖它。
.equals() String
.equals()
Object
String
equals() 方法比较两个字符串的值相等性,无论它们在逻辑上是否相等。
equals() String类中的方法将另一个字符串作为参数,并将其与指定的字符串进行比较。true 当且仅当参数字符串不为null且包含与指定字符串相同的字符时,它返回 。
equals() String
true
public boolean equals(Object anObject) It compare this string with the argument strings and return true if the argument is not null and contains the same character as the specified string. param - another string returns - true - if argument is not null and it contains same characters as the specified string false - if the argument is null or it does not contain same characters as the specified string ex. firstString.equals(secondString) // returns true if and only if the secondString is not null and contains the same characters as firstString.
我已经要求程序使用以下equals() 方法比较字符串 :
/** * A Java program to compare two strings using equsls() * and equalsIgnoreCase() method of the String. * * @author Gaurav Kukade at coderolls.com */ public class CompareUsingEquals { public static void main(String[] args) { String firstString = "coderolls"; String secondString = "javablog"; String thirdString = "coderolls"; String fourthString = "CodeRolls"; System.out.println("Comparing strings using equals() and equalsIgnoreCase() method\n"); // Using equals() method System.out.print("firstString.equals(secondString) : "); System.out.println(firstString.equals(secondString)); System.out.print("firstString.equals(thirdString) : "); System.out.println(firstString.equals(thirdString)); /* * Using equalsIgnoreCase() method to ignore * case consideration (i.e. Capital or small) of both the strings. */ System.out.print("firstString.equalsIgnoreCase(fourthString) : "); System.out.println(firstString.equalsIgnoreCase(fourthString)); } }
输出:
Comparing strings using equals() and equalsIgnoreCase() method firstString.equals(secondString) : false firstString.equals(thirdString) : true firstString.equalsIgnoreCase(fourthString) : true
2.使用==运算符比较字符串 在String中, ==运算符用于比较给定字符串的引用,具体取决于它们是否引用相同的对象。
当您使用==运算符比较两个字符串时 ,true 如果字符串变量指向相同的java对象,它将返回 。否则,它将返回 false。
false
我给了一个Java程序,使用== 下面的运算符进行比较 :
/** * A Java program to compare strings using == operator. * * == operator ckecks whether both the strings referring * to the same String Object. * * @author Gaurav Kukade at coderolls.com */ public class CompareUsingEqualsToOperator { public static void main(String[] args) { String firstString = "coderolls"; String secondString = "javablog"; String thirdString = "coderolls"; // creating new String object with the same value as firstString or thirdString String fourthString = new String("coderolls"); System.out.println("Comparing strings using == operator \n"); System.out.print("firstString == secondString : "); System.out.println(firstString == secondString); /* * Here firstString and thirdString is referring to the same String object * hence it will print 'true'. */ System.out.print("firstString == thirdString : "); System.out.println(firstString == thirdString); /* * Here firstString and fourthString have same value * but they are referring to the different String object. * * hence it will print 'false' */ System.out.print("firstString == fourthString : "); System.out.println(firstString == fourthString); } }
Comparing strings using == operator firstString == secondString : false firstString == thirdString : true firstString == fourthString : false
使用 == 运算符进行字符串比较时出现问题 大多数初学者Java开发人员都会通过使用==运算符比较两个字符串来犯此错误。
从逻辑上讲,他们必须检查两个字符串是否包含相同的字符序列。
在Java字符串中, == 运算符用于检查两个字符串对象的引用以及 equals() 用于检查两个字符串的值相等性的方法。
== –检查引用是否相等
equals() –检查值是否相等
当我们为字符串变量分配一个字符串值时,JVM将检查字符串池中是否已经存在具有相等值的字符串。如果它在字符串池中不存在,它将被添加到常量池中,并返回对该字符串对象的引用。
如果它存在于字符串池中,则返回对该字符串对象的内存地址的引用。
下图显示了相同的图片说明。
在上方,我们firstString指向字符串池中的“ coderolls”字符串。 如果我们将相等的值分配给另一个字符串变量,则JVM将检查具有该值的字符串是否存在于字符串常量池中。
由于具有该值的字符串对象已在上一步中创建,因此另一个字符串变量开始引用先前创建的字符串对象实例。
下图显示了对指向字符串池中“ coderolls”字符串的“ firstString”和“ secondString”的图形说明。
“ coderolls”
“ firstString”
“ secondString”
当我们使用new 运算符创建字符串时,将创建一个 新的字符串对象并将其存储在Java堆空间中。
new
在上方,我们可以看到' firstString'和' secondString'指向coderolls字符串池中的“ ”字符串,而' thirdString'指向coderollsJava堆空间中的“ ”。 如果参数字符串在字典上大于指定的字符串,即参数字符串在指定的字符串之后,则它返回负整数。(argument String > specified String) 如果参数字符串在字典上小于指定的字符串,即参数字符串在指定的字符串之前,则它返回正整数。(argument String < specified String )
' firstString'
' secondString'
coderolls
' thirdString'
coderollsJava
如果两个字符串在字典上都相等,则返回零。(argument String = specified String)
如果要忽略两种情况下的字符串使用 compareToIgnoreCase() 方法。
compareToIgnoreCase()
我已经给出了使用该compareTo() 方法比较字符串的程序 。它还包含一个案例,用于忽略带compareToIgnoreCase() 方法的案例 。
/** * A Java program to compare strings using compareTo() * and compareToIgnoreCase() method. * * compareTo() compare strings lexicograpgically. * * @author Gaurav Kukade at coderolls.com */ public class CompareUsingCompareTo { public static void main(String[] args) { String firstString = "java"; String secondString = "coderolls"; String thirdString = "sql"; String fourthString = "CodeRolls"; System.out.println("Comparing strings using compareTo() and compareToIgnoreCase() method\n"); // Using compareTo() method System.out.print("firstString.compareTo(secondString) : "); System.out.println(firstString.compareTo(secondString)); System.out.print("firstString.compareTo(thirdString) : "); System.out.println(firstString.compareTo(thirdString)); /* * Using compareToIgnoreCase() method to ignore * case consideration (i.e. Capital or small) of both the strings. */ System.out.print("secondString.compareToIgnoreCase(fourthString) : "); System.out.println(secondString.compareToIgnoreCase(fourthString)); } }
Comparing strings using compareTo() and compareToIgnoreCase() method firstString.compareTo(secondString) : 7 firstString.compareTo(thirdString) : -9 secondString.compareToIgnoreCase(fourthString) : 0
如果您想了解更多信息,我写了一篇详细的文章, 介绍如何在Java中按字典顺序比较字符串。
结论 我们可以使用以下给出的方法比较字符串:
using equals() method: equals()
:==
始终建议使用equals()method根据字符串的内容进行比较。
equals()method
如果您对以上给出的代码块有任何疑问,请在下面的注释部分中写下来。另外,在注释部分中,让我知道您是否还有其他方法可以比较Java中的两个字符串。
原文链接:https://codingdict.com/