Java program to compare two strings are equal by ignoring the case

Views: 139   

Following program shows you how to compare two strings are equal by ignoring the case.
In this program we get two different cases of strings from user and shows those strings are equal by using .equalsIgnoreCase()

 import java.util.Scanner;

public class StringOperations7 {

    public static void main(String[] args) {
        String string1;
        String string2;

        Scanner in = new Scanner(System.in);

        System.out.println("Please enter string1:");
        string1 = in.nextLine();

        System.out.println("Please enter string2:");
        string2 = in.nextLine();

        if (string1.equalsIgnoreCase(string2)) {

            System.out.println("Given strings are equal");

        } else {

            System.out.println("Given strings are not equal");

        }
    }
}

Output:

Example1:

Please enter string1:
HELLO
Please enter string2:
hello
Given strings are equal

Example2:

Please enter string1:
hello
Please enter string2:
world
Given strings are not equal
On By