How do we make my string comparison case insensitive in java?


We can compare Strings in Java in various ways −

  • Using the comapareTo() method − The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.

Example

 Live Demo

import java.util.Scanner;
public class StringComparison {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter string1: ");
      String str1 = sc.next();
      System.out.println("Enter string2: ");
      String str2 = sc.next();
      int result = str1.compareTo(str2);
      if (result < 0) {
         System.out.println("str1 is not equal to str2");
      } else if (result == 0) {
         System.out.println("str1 is equal to str2");
      } else {
         System.out.println("str1 is not equal to str2");
      }
   }
}

Output1

Enter string1:
Hello
Enter string2:
Hello
str1 is equal to str2

Output2

Enter string1:
hello
Enter string2:
hi
str1 is not equal to str2
  • Using the == operator − You can compare two strings using == operator. But, it compares references of the given variables not values.

Example

 Live Demo

import java.util.Scanner;
public class StringComparison {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      String str1 = "hello";
      String str2 = "hello";
      if (str1 == str2 ){
         System.out.println("Both are equal");
      } else {
         System.out.println("Both are not equal");
      }
   }
}

Output

Both are equal
  • Using the equals() method − of the String class accepts a String as parameter and it compares the current string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object including case.

Example

 Live Demo

import java.util.Scanner;
public class StringComparison {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter string1: ");
      String str1 = sc.next();
      System.out.println("Enter string2: ");
      String str2 = sc.next();
      boolean bool = str1.equals(str2);
      if (bool) {
         System.out.println("Both are equal");
      } else {
         System.out.println("Both are not equal");
      }
   }
}

Output1

Enter string1:
Hello
Enter string2:
hello
Both are not equal

Output2

Enter string1:
Hello
Enter string2:
Hello
Both are equal

String comparison case insensitive

The equalsIgnoreCase() method of the String class is similar to the equals() method the difference if this method compares the given string to the current one ignoring case.

Example

 Live Demo

import java.util.Scanner;
public class StringComparison {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter string1: ");
      String str1 = sc.next();
      System.out.println("Enter string2: ");
      String str2 = sc.next();
      boolean bool = str1.equalsIgnoreCase(str2);
      if (bool) {
         System.out.println("Both are equal");
      } else {
         System.out.println("Both are not equal");
      }
   }
}

Output1

Enter string1:
Hello
Enter string2:
hello
Both are equal
raja
Published on 11-Oct-2019 11:50:03
Advertisements