Java String equalsIgnoreCase()方法

2019-10-16 22:21:47

Java String equalsIgnoreCase()方法將此字串與指定的物件進行比較並忽略大小寫。如果它們具有相同的長度,並且兩個字串中的相應字元等於忽略大小寫,則結果返回:true,否則返回:false

語法

以下是此方法的語法 -

public boolean equalsIgnoreCase(String anotherString)

引數

  • anObject - 用於與此String進行比較的String。

返回值

  • 如果與String引數值忽略大小寫比較相等時,則此方法返回true; 否則返回false

範例

public class Test {

   public static void main(String args[]) {
      String Str1 = new String("This is really not immutable!!");
      String Str2 = Str1;
      String Str3 = new String("This is really not immutable!!");
      String Str4 = new String("This IS REALLY NOT IMMUTABLE!!");
      boolean retVal;

      retVal = Str1.equals( Str2 );
      System.out.println("Returned Value = " + retVal );

      retVal = Str1.equals( Str3 );
      System.out.println("Returned Value = " + retVal );

      retVal = Str1.equalsIgnoreCase( Str4 );
      System.out.println("Returned Value = " + retVal );
   }
}

執行上面範例程式碼,得到以下結果:

Returned Value = true
Returned Value = true
Returned Value = true