JAVA program to compare two strings using string method equalsIgnoreCase()

JAVA program to compare two strings using string method equalsIgnoreCase()

This JAVA program is to compare two strings using string method equalsIgnoreCase(String).

String method equalsIgnoreCase() compares the two strings.

This method is similar to the equals() method with only difference is that equalsIgnoreCase() method is not case sensitive, that is if we have two strings let’s say s1 = ‘code’ and s2 = ‘CODE’, this method will yet return true as it ignores the case(whether the character is lowercase or upper case).

Return boolean values str1.equalsIgnoreCase(str2)

  • True if both strings have same content.
  • False if both strings have different content (ignores whether character is uppercase or lowercase)

Note :- When we compare strings in java do not use “==” operator as it compares the reference to the objects and not the content of the strings.

Logic

This Java method str1.equalsIgnoreCase(str2) will compare string str1 with string str2.

Any other further queries, you can leave it in the comment box.

Program

import java.util.*;

class equalsIgnore
{
	public static void main(String args[])
	{
		String str1,str2;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the 1st string");
	 	str1=sc.nextLine();
		System.out.println("Enter the 2nd string");
	 	str2=sc.nextLine();
		
		if(str1.equalsIgnoreCase(str2))
    		{
 	        	System.out.println("The two string are EQUAL!!!");
        		System.out.println(str1+" = "+str2+"("+(str1.equalsIgnoreCase(str2))+")");
    		}

    		else
    		{
 	        	System.out.println("The two string are NOT EQUAL!!!");
        		System.out.println(str1+" != "+str2+"("+(str1.equalsIgnoreCase(str2))+")");
    		}
	}
}

Output

Share Me!