Java - String equals() 方法



Java String equals() 方法用於檢查當前字串是否等於指定物件。如果字串例項包含相同順序的相同字元,則返回 true;否則返回 false。

equals() 方法接受一個可比較的物件作為引數。在比較或檢查字串是否等於指定物件時,它不會丟擲任何異常。此方法返回布林值。

語法

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

public boolean equals(Object anObject)

引數

  • anObject − 這是與該字串進行比較的物件。

返回值

如果給定物件表示一個與該字串等效的字串,則此方法返回 true,否則返回 false。

使用相同字串字面量檢查字串物件示例

如果給定的字串值與指定的物件值相同,則 equals() 方法返回true

在下面的程式中,我們使用值“Java”例項化 String 類。使用equals() 方法,我們嘗試確定給定的字串是否等於指定的物件“Java”

package com.tutorialspoint;

public class Equal {
   public static void main(String[] args) {
      
      //instantiate a String class
      String str = new String("Java");
      
      //initialize the string object
      String obj = "Java";
      System.out.println("The given string is: " + str);
      System.out.println("The object is: " + obj);
      
      //using the equals() method
      System.out.println("The given string is equal to the specified object or not? " + str.equals(obj));
   }
}

輸出

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

The given string is: Java
The object is: Java
The given string is equal to the specified object or not? true

使用不同字串字面量檢查字串物件示例

如果給定的字串值與指定的物件值不同,則 equals() 方法返回false

在下面的示例中,我們建立一個值為“HelloWorld”的字串字面量。使用equals() 方法,我們嘗試確定給定的字串是否等於指定的物件“Hello”

package com.tutorialspoint;

public class Equal {
   public static void main(String[] args) {
      
      //create a String literal
      String str = "HelloWorld";
      
      //initialize the string object
      String obj = "Hello";
      System.out.println("The given string is: " + str);
      System.out.println("The object is: " + obj);
      
      //using the equals() method
      System.out.println("The given string is equal to the specified object or not? " + str.equals(obj));
   }
}

輸出

以下是上述程式的輸出:

The given string is: HelloWorld
The object is: Hello
The given string is equal to the specified object or not? false

使用相同字串字面量檢查字串字面量示例

使用條件語句來確定給定的字串是否等於指定的物件。

在這個程式中,我們建立一個值為“TutorialsPoint”的字串。使用equals() 方法和條件語句,我們嘗試檢查給定的字串是否等於指定的物件“Java”

package com.tutorialspoint;

public class Equal {
   public static void main(String[] args) {
      
      //create a String literal
      String str = "TutorialsPoint";
      
      //initialize the string object
      String obj = "Java";
      System.out.println("The given string is: " + str);
      System.out.println("The object is: " + obj);
      
      //using the equals() method
      boolean bool = str.equals(obj);
      if(bool) {
         System.out.println("The given string is equal to the specified object.");
      } else {
         System.out.println("The given string is not equal to the specified object.");
      }
   }
}

輸出

上述程式產生以下輸出:

The given string is: TutorialsPoint
The object is: Java
The given string is not equal to the specified object.

使用不同字串字面量檢查字串字面量示例

如果給定的字串值與物件值相同,但大小寫不同,則此方法返回false

在這個程式中,我們建立一個值為“TutorialsPoint”的字串字面量。使用equals() 方法,我們嘗試檢查給定的字串是否等於指定的物件“tutorialspoint”。由於該方法區分大小寫,即使值相同,此方法也會返回 false。

package com.tutorialspoint;

public class Equal {
   public static void main(String[] args) {
      
      //create a String literal
      String str = "TutorialsPoint";
      
      //initialize the string object
      String obj = "tutorialspoint";
      System.out.println("The given string is: " + str);
      System.out.println("The object is: " + obj);
      
      //using the equals() method
      boolean bool = str.equals(obj);
      System.out.println("The equals() method returns " + bool);
      if(bool) {
         System.out.println("The given string is equal to the specified object.");
      } else {
         System.out.println("The given string is not equal to the specified object.");
      }
   }
}

輸出

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

The given string is: TutorialsPoint
The object is: tutorialspoint
The equals() method returns false
The given string is not equal to the specified object.
java_lang_string.htm
廣告
© . All rights reserved.