Java字符串比较

来源:互联网 发布:淘宝转运香港 编辑:程序博客网 时间:2024/05/21 07:10

String类覆盖了Object类的equals()方法,并提供了自己的实现,equals()方法是根据它们的内容比较两个字符串的相等性。

等于(相等)

例如,可以比较两个字符串的相等性,如下所示:

String str1 = new String("Hello"); String str2 = new String("Hi"); String str3 = new String("Hello");boolean b1,   b2;b1  = str1.equals(str2); // false will be  assigned to b1 b2  = str1.equals(str3); // true will be  assigned to b2
Java

还可以将字符串字面量与字符串字面量,或字符串对象进行比较,如下所示:

b1  = str1.equals("Hello");   // true will be  assigned to b1 b2  = "Hello".equals(str1);   // true will be  assigned to b2 b1  = "Hello".equals("Hi");   // false will be  assigned to b1
Java

==操作符总是比较内存中两个对象的引用。str1 == str2str1 == str3将返回false,因为str1str2str3是内存中三个不同String对象的引用。

比较

要根据字符的Unicode值比较两个字符串,请使用compareTo()方法。 它的签名是 -

public int compareTo(String anotherString)
Java

它返回一个整数,它可以是0(零),正整数或负整数。此方法返回这两个字符的Unicode值之间的差值。

例如,"a".compareTo("b")将返回-1。 a的Unicode值为97b98。它返回差值97 - 98,所以它返回的差值是:-1
以下是字符串比较的示例:

"abc".compareTo("abc") will  return  0"abc".compareTo("xyz") will  return  -23  (value of  'a' -  'x') "xyz".compareTo("abc") will  return  23  (value of  'x' -  'a')
Java

以下代码显示如何进行字符串比较。

public class Main {  public static void main(String[] args) {    String apple = new String("Apple");    String orange = new String("Orange");    System.out.println(apple.equals(orange));    System.out.println(apple.equals(apple));    System.out.println(apple == apple);    System.out.println(apple == orange);    System.out.println(apple.compareTo(apple));    System.out.println(apple.compareTo(orange));  }}
Java

上面的代码生成以下结果。

falsetruetruefalse0-14
Java

字符串池

Java维护一个所有字符串文字的池。它在字符串池中为每个字符串文字创建一个String对象。当遇到字符串字面量时,它在字符串池中查找具有相同内容的字符串对象。 如果在字符串池中找不到匹配项,它将创建一个新的String对象并将其添加到字符串池中。

如果它在字符串池中找到匹配项,它将使用池中找到的String对象的引用替换字符串字面值。
可以使用intern()方法向字符串池添加一个String对象。
如果找到匹配,intern()方法从字符串池返回对象的引用。 否则,它将一个新的String对象添加到字符串池,并返回新对象的引用。

字符串大小写比较

要比较两个字符串是否相等并忽略它们的大小,请使用equalsIgnoreCase()方法。要对两个字符串执行区分大小写的比较,请使用equals()方法。

public class Main {  public static void main(String[] args) {    String str1 = "Hello";    String str2 = "HELLO";    if (str1.equalsIgnoreCase(str2)) {      System.out.println("Ignoring case str1 and str2 are equal");    } else {      System.out.println("Ignoring case str1 and str2 are not equal");    }    if (str1.equals(str2)) {      System.out.println("str1 and str2 are equal");    } else {      System.out.println("str1 and str2 are not equal");    }  }}
Java

上面的代码生成以下结果。

Ignoring case str1 and str2 are equalstr1 and str2 are not equal
Java

语言敏感字符串比较

String类根据字符的Unicode值比较字符串。要根据字典顺序比较字符串,请使用java.text.Collator类的compare()方法执行语言敏感(字典顺序)字符串比较。

该方法需要两个字符串作为参数进行比较。 如果两个字符串相同,返回0,如果第一个字符串在第二个字符串之后返回1,如果第一个字符串在第二个字符串之前,返回-1

以下代码说明了Collator类的使用。

import java.text.Collator;import java.util.Locale;public class Main {  public static void main(String[] args) {    Locale USLocale = new Locale("en", "US");    Collator c = Collator.getInstance(USLocale);    String str1 = "Java";    String str2 = "HTML";    int diff = c.compare(str1, str2);    System.out.print("Comparing using Collator  class: ");    if (diff > 0) {      System.out.println(str1 + "  comes after " + str2);    } else if (diff < 0) {      System.out.println(str1 + "  comes before " + str2);    } else {      System.out.println(str1 + "  and  " + str2 + "  are   the   same.");    }  }}
Java

上面的代码生成以下结果。

Comparing using Collator  class: Java  comes after HTML
0 0
原创粉丝点击