如何在Java中比较字符串?

在本文中,优锐课将带你学习如何比较字符串以及使用等于(=)运算符比较字符串时发生的问题。

介绍

字符串是Java中的特殊类。我们在Java程序中定期使用String,因此比较两个字符串是Java中的一种常见做法。在本文中,我试图回答有关字符串的最常见问题,例如:“如何比较Java中的字符串?”

在身份验证,排序,引用匹配等过程中,比较字符串非常有帮助。

我列出了三种比较Java中字符串的方法。

1.     使用equals()方法(比较内容)

2.     使用==运算符(比较对象引用)

3.     使用compareTo()方法(按字典顺序比较字符串)

1.使用Equals()方法比较字符串

这样,我正在使用String类的.equals()实例方法。最初.equals()方法是Object类方法,String类覆盖了它。

equals()方法比较两个字符串的值相等性,无论它们在逻辑上是否相等。

String类中的equals()方法将另一个字符串作为参数,并将其与指定的字符串进行比较。当且仅当参数字符串不为null且包含与指定字符串相同的字符时,它返回true

1

public boolean equals(Object anObject)

2

3

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.

4

5

param -

6

another string

7

8

returns -

9

10

true - if argument is not null and it contains same characters as the specified string

11

false - if the argument is null or it does not contain same characters as the specified string

12

13

ex. firstString.equals(secondString)

14

// returns true if and only if the secondString is not null and contains the same characters as firstString.

我已经要求程序使用下面的equals()方法比较字符串:

1

/**

2

 * A Java program to compare two strings using equsls()

3

 * and equalsIgnoreCase() method of the String.

4

 *

5

 * @author Gaurav Kukade at coderolls.com

6

 */

7

8

public class CompareUsingEquals {

9

10

  public static void main(String[] args) {

11

    String firstString = "coderolls";

12

    String secondString = "javablog";

13

    String thirdString = "coderolls";

14

    String fourthString = "CodeRolls";

15

16

    System.out.println("Comparing strings using equals() and equalsIgnoreCase() method\n");

17

18

    // Using equals() method

19

    System.out.print("firstString.equals(secondString) : ");

20

    System.out.println(firstString.equals(secondString));

21

22

    System.out.print("firstString.equals(thirdString) : ");

23

    System.out.println(firstString.equals(thirdString));

24

25

    /*

26

     * Using equalsIgnoreCase() method to ignore

27

     * case consideration (i.e. Capital or small) of both the strings.

28

     */

29

    System.out.print("firstString.equalsIgnoreCase(fourthString) : ");

30

    System.out.println(firstString.equalsIgnoreCase(fourthString));

31

  }

32

33

}

 

Output:

1

Comparing strings using equals() and equalsIgnoreCase() method

2

3

firstString.equals(secondString) : false

4

firstString.equals(thirdString) : true

5

firstString.equalsIgnoreCase(fourthString) : true

2.使用==运算符比较字符串

String中,==运算符用于比较给定字符串的引用,具体取决于它们是否引用相同的对象。

当使用==运算符比较两个字符串时,如果字符串变量指向同一个Java对象,它将返回true。否则,它将返回false

我给了一个Java程序,可以在下面使用==运算符进行比较:

1

/**

2

 * A Java program to compare strings using == operator.

3

 *

4

 * == operator ckecks whether both the strings referring

5

 * to the same String Object.

6

 *

7

 * @author Gaurav Kukade at coderolls.com

8

 */

9

10

public class CompareUsingEqualsToOperator {

11

12

  public static void main(String[] args) {

13

14

    String firstString = "coderolls";

15

    String secondString = "javablog";

16

    String thirdString = "coderolls";

17

18

    // creating new String object with the same value as firstString or thirdString

19

    String fourthString =  new String("coderolls");

20

21

    System.out.println("Comparing strings using == operator \n");

22

23

    System.out.print("firstString == secondString : ");

24

    System.out.println(firstString == secondString);

25

26

    /*

27

     * Here firstString and thirdString is referring to the same String object

28

     * hence it will print 'true'.

29

     */

30

    System.out.print("firstString == thirdString : ");

31

    System.out.println(firstString == thirdString);

32

33

    /*

34

     * Here firstString and fourthString have same value

35

     * but they are referring to the different String object.

36

     *

37

     * hence it will print 'false'

38

     */

39

    System.out.print("firstString == fourthString : ");

40

    System.out.println(firstString == fourthString);

41

  }

42

43

}

 

Output:

1

Comparing strings using == operator

2

3

firstString == secondString : false

4

firstString == thirdString : true

5

firstString == fourthString : false

 

 

使用==运算符进行字符串比较时出现问题

大多数初学者Java开发人员通过使用== operator比较两个字符串来犯此错误。

从逻辑上讲,他们必须检查两个字符串是否包含相同的字符序列。

Java字符串中,==运算符用于检查两个字符串对象的引用,而equals()方法用于检查两个字符串的值相等。

== –检查引用是否相等

equals()–检查值是否相等

当我们为字符串变量分配字符串值时,JVM将检查字符串池中是否已经存在具有相等值的字符串。 如果它在字符串池中不存在,它将被添加到常量池中,并返回对该字符串对象的引用。

如果它存在于字符串池中,则返回对该字符串对象的内存地址的引用。

下图显示了相同的图片说明。

image.png

上面,我们有“ firstString”指向字符串池中的“ coderolls”字符串。

如果我们将相等的值分配给另一个字符串变量,则JVM将检查具有该值的字符串是否存在于字符串常量池中。

由于具有该值的字符串对象已在上一步中创建,因此另一个字符串变量开始引用先前创建的字符串对象实例。

下图显示了指向字符串池中“ coderolls”字符串的“ firstString”和“ secondString”的图形说明。

image.png

当我们使用new运算符创建字符串时,将创建一个新的字符串对象并将其存储在Java堆空间中。

image.png

在上方,我们可以看到“ firstString”和“ secondString”指向字符串池中的“ coderolls”字符串,而“ thirdString”指向Java堆空间中的“ coderolls”。

如果参数字符串在字典上大于指定的字符串,即参数字符串在指定的字符串之后,则返回负整数。(参数String>指定的String

如果参数字符串在字典上小于指定的字符串,即参数字符串在指定的字符串之前,则返回正整数。(参数String <指定的String)

如果两个字符串在字典上都相等,则返回零。(参数String =指定的String)

如果要忽略两个字符串的大小写,请使用compareToIgnoreCase()方法。

我提供了一个使用compareTo()方法比较字符串的程序。它还包括一个用compareToIgnoreCase()方法忽略大小写的大小写。

1

/**

2

 * A Java program to compare strings using compareTo()

3

 * and compareToIgnoreCase() method.

4

 *

5

 * compareTo() compare strings lexicograpgically.

6

 *

7

 * @author Gaurav Kukade at coderolls.com

8

 */

9

10

public class CompareUsingCompareTo {

11

12

  public static void main(String[] args) {

13

    

14

    String firstString = "java";

15

    String secondString = "coderolls";

16

    String thirdString = "sql";

17

    String fourthString = "CodeRolls";

18

    

19

    System.out.println("Comparing strings using compareTo() and compareToIgnoreCase() method\n");

20

    

21

    // Using compareTo() method

22

    System.out.print("firstString.compareTo(secondString) : ");

23

    System.out.println(firstString.compareTo(secondString));

24

    

25

    System.out.print("firstString.compareTo(thirdString) : ");

26

    System.out.println(firstString.compareTo(thirdString));

27

    

28

    /*

29

     * Using compareToIgnoreCase() method to ignore

30

     * case consideration (i.e. Capital or small) of both the strings.

31

     */

32

    System.out.print("secondString.compareToIgnoreCase(fourthString) : ");

33

    System.out.println(secondString.compareToIgnoreCase(fourthString));

34

  }

35

36

}

 

Output:

1

Comparing strings using compareTo() and compareToIgnoreCase() method

2

3

firstString.compareTo(secondString) : 7

4

firstString.compareTo(thirdString) : -9

5

secondString.compareToIgnoreCase(fourthString) : 0

 


结论

我们可以使用以下给出的方法比较字符串:

1.     使用equals()方法:用于检查字符串值是否相等的字符串中的equals()方法是否包含相同的字符序列。

2.     使用==运算符:==运算符用于检查两个字符串的引用相等性,即它们是否指向同一字符串对象。

3.     使用compareTo()方法:compareTo()方法用于按字典顺序(即按字母顺序)检查字符串。查看有关如何按字典顺序比较字符串的详细文章。

大多数初学者Java开发人员在比较字符串时都会出错。他们想检查字符串的内容,但是他们使用==运算符检查它。

始终建议使用equals()方法根据字符串的内容比较字符串。

如果你对上面给出的代码块有任何疑问,请在下面的注释部分写下来。另外,在注释部分中,让我知道你是否还有其他方法可以比较Java中的两个字符串。

 

感谢阅读!

另外近期整理了一套完整的java架构思维导图,分享给同样正在认真学习的每位朋友~

课程大纲优锐课水印简版_副本_副本.jpg

猜你喜欢

转载自blog.51cto.com/14667019/2469768