Java字符串比较

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

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

false
true
true
false
0
-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 equal
str1 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java字符串比较可以使用多种方法,其中最常用的是equals()方法。该方法比较两个字符串的内容是否相同,而不是比较它们的引用是否相同。因此,即使两个字符串对象的引用不同,只要它们的内容相同,equals()方法就会返回true。 以下是Java字符串比较的三种方法: 1.使用equals()方法比较字符串内容是否相同。 ```java String str1 = "hello"; String str2 = "world"; if(str1.equals(str2)){ System.out.println("str1和str2的内容相同");}else{ System.out.println("str1和str2的内容不同"); } ``` 2.使用compareTo()方法比较字符串的字典顺序。该方法返回一个整数,如果字符串在字典中排在参数字符串之前,则返回负数;如果字符串在字典中排在参数字符串之后,则返回正数;如果两个字符串相等,则返回0。 ```java String str1 = "hello"; String str2 = "world"; int result = str1.compareTo(str2); if(result < 0){ System.out.println("str1在字典中排在str2之前"); }else if(result > 0){ System.out.println("str1在字典中排在str2之后"); }else{ System.out.println("str1和str2在字典中的位置相同"); } ``` 3.使用==运算符比较两个字符串的引用是否相同。如果两个字符串的引用指向同一个对象,则返回true;否则返回false。 ```java String str1 = "hello"; String str2 = "hello"; if(str1 == str2){ System.out.println("str1和str2的引用相同"); }else{ System.out.println("str1和str2的引用不同"); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值