Java - String String Equality

Introduction

To compare two strings for equality ignoring their cases, use the equalsIgnoreCase() method.

To perform a case-sensitive comparison for equality, use the equals() method.

Demo

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 {/*from  w ww .j ava  2  s .c  o m*/
      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");
    }

  }
}

Result

Related Topics

Exercise