Java - String contentEquals() 方法



描述

Java String contentEquals() 方法用於比較當前字串與給定的字元序列。此方法返回一個布林值,當且僅當此字串表示與指定序列相同的 char 值序列時返回 true;否則返回 false。

contentEquals() 方法接受兩個引數,分別是 CharSequence 和 StringBuffer,它們分別儲存 cs 和 sb 值。在將當前字串與指定的字元序列和字串緩衝區進行比較時,它不會丟擲任何異常。

CharSequence 是字元值的易讀序列。字串是字元的不可變序列,並實現 CharSequence 介面。StringBuffer 與字串類似,但它是可修改的且同步的。

contentEquals() 方法有兩個多型變體,它們具有不同的引數:CharSequence 和 StringBuffer(以下是所有多型變體的語法)。

語法

以下是Java String contentEquals() 方法的語法:

public boolean contentEquals(CharSequence cs)  
public Boolean contentEquals(StringBuffer sb) 

引數

  • cs − 這是要與該字串進行比較的字元序列。

  • sb − 這是要與該字串進行比較的 StringBuffer 序列。

返回值

此方法返回一個布林值,如果當前字串與給定物件相等則為 True,否則為 false。

使用 CharSequence 示例檢查匹配字串

如果我們將CharSequence作為引數傳遞給此方法,並且它與當前字串具有相同的值,則 contentEquals() 方法將返回true

在下面的程式中,我們使用值為"Hello"建立了一個字串字面量。然後,我們使用相同的值"Hello"建立了一個 CharSequence。由於兩者具有相同的值,因此當我們使用contentEquals()方法比較它們時,此方法將返回 true。

package com.tutorialspoint;

public class TP {
   public static void main(String[] args) {
      
      // create a string literal
      String str1 = "Hello";
      System.out.println("The String is: " + str1);
      
      //create a charSequence
      CharSequence ch = "Hello";
      System.out.println("The given char sequence is: " + ch);
      System.out.println("The given string is equal to the specified sequence or not? " + str1.contentEquals(ch));
   }
}

輸出

執行上述程式後,將產生以下結果:

The String is: Hello
The given char sequence is: Hello
The given string is equal to the specified sequence or not? true

使用 CharSequence 示例檢查不匹配字串

如果我們將 CharSequence 作為引數傳遞給方法,並且它與當前字串的值不同,則此方法將返回false

在下面的程式中,我們使用值為"TutorialsPoint"建立了一個字串類的物件。然後,使用contentEquals()方法,我們嘗試比較指定字元序列處的字串。由於兩者具有不同的值,因此該方法將返回false

package com.tutorialspoint;

public class TP {
   public static void main(String[] args) {
      
      // create an object of the String  class
      String str = new String("TutorialsPoint");
      
      // CharSequance declaration
      CharSequence cs = "Point";
      System.out.println("The string is: " + str);
      System.out.println("The CharSequence is: " + cs);
      
      // declare boolean variable to hold the method value
      boolean bool = str.contentEquals(cs);
      System.out.println("The given string is equal to the specified sequence or not? " + bool);
   }
}

輸出

以下是上述程式的輸出:

The string is: TutorialsPoint
The CharSequence is: Point
The given string is equal to the specified sequence or not? false

使用 StringBuffer 示例檢查匹配字串

如果我們將StringBuffer作為引數傳遞給方法,並且它與字串具有相同的值,則 contentEquals() 方法將返回true

在下面的程式中,我們使用值為"Hello World"例項化字串類。然後,我們使用值為"Hello World"建立了一個 StringBuffer 類的物件。使用contentEquals()方法,我們嘗試比較指定 StringBuffer 處的字串。由於兩者具有相同的值,因此 contentEquals() 方法將返回true

package com.tutorialspoint;

public class TP {
   public static void main(String[] args) {
      
      // Instantiate the String class
      String str = new String("Hello World");
      
      // create an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("Hello World");
      System.out.println("The string is: " + str);
      System.out.println("The StringBuffer is: " + sb);
      
      // using contentEquals() method
      System.out.println("The given string is equal to the specified StringBuffer sequence or not? " + str.contentEquals(sb));
   }
}

輸出

上述程式產生以下輸出:

The string is: Hello World
The StringBuffer is: Hello World
The given string is equal to the specified StringBuffer sequence or not? true

使用 CharSequence 示例檢查不匹配字串

在此程式中,我們使用值為"JavaScript"建立了一個字串字面量。然後,我們使用值為"Java"建立了一個 StringBuffer。使用contentEquals()方法,我們嘗試將字串與指定的 StringBuffer 進行比較。

package com.tutorialspoint;

public class TP {
   public static void main(String[] args) {
      
      // create a string literal
      String str ="JavaScript";
      
      // Instantiate the StringBuffer class
      StringBuffer sb = new StringBuffer("Java");
      System.out.println("The string is: " + str);
      System.out.println("The StringBuffer is: " + sb);
      
      // using contentEquals() method
      boolean bool = str.contentEquals(sb);
      if(bool) {
         System.out.println("The given string is equal to the specified StringBuffer sequence");
      } else {
         System.out.println("The given string is not equal to the specified StringBuffer sequence");
      }
   }
}

輸出

執行上述程式後,將生成以下輸出:

The string is: JavaScript
The StringBuffer is: Java
The given string is not equal to the specified StringBuffer sequence

使用 contentEquals() 方法檢查字串時出現異常的示例

如果給定的字串值為null,則此方法將丟擲NullPointerException

在下面的示例中,我們使用null 值建立了一個字串。然後,我們使用值為"Java"例項化 StringBuffer 類。使用contentEquals()方法,我們嘗試將給定的字串與指定的 StringBuffer 進行比較。

package com.tutorialspoint;

public class Demo {
   public static void main(String[] args) {
      try {
         
         // create a string literal
         String str = null;
         
         // Instantiate the StringBuffer class
         StringBuffer sb = new StringBuffer("Java");
         System.out.println("The string is: " + str);
         System.out.println("The StringBuffer is: " + sb);
         
         // using contentEquals() method
         boolean bool = str.contentEquals(sb);
         if(bool) {
            System.out.println("The given string is equal to the specified StringBuffer sequence");
         } else {
            System.out.println("The given string is not equal to the specified StringBuffer sequence");
         }
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exceptipn: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The string is: null
java.lang.NullPointerException: Cannot invoke "String.contentEquals(StringBuffer)" because "str" is null
The StringBuffer is: Java
	at com.tutorialspoint.String.Demo.main(Demo.java:13)
Exceptipn: java.lang.NullPointerException: Cannot invoke "String.contentEquals(StringBuffer)" because "str" is null
java_lang_string.htm
廣告
© . All rights reserved.