Java compareToIgnoreCase

The Java compareToIgnoreCase method is one of the Java String Methods, which is to compare the string with user-specified string lexicographically, and ignores the case difference. In this article, we will show how to write compareToIgnoreCase in Java Programming language with example. The syntax of the String.compareToIgnoreCase in Java Programming language is as shown below.

Java compareToIgnoreCase Method syntax

The following method will accept the String data as an argument and compare both the strings to check whether they are lexicographically equal or not. While comparing the two strings, it will ignore the case difference.

public int compareToIgnoreCase(String Str); // It will return integer 

//In order to use in program
String_Object.compareToIgnoreCase(String Str)

The Java String.compareToIgnoreCase method will lexicographically compare the character sequence of String_Object with the character sequence of Str by ignoring the case difference (Case Sensitivity).

  • If the String_Object lexicographically precedes the string Str String.compareToIgnoreCase will return the result as Negative Integer.
  • If the String_Object lexicographically follows the string Str, Java String.compareToIgnoreCase will return the result as Positive Integer.
  • And if the String_Object and the string Str are equal, compareToIgnoreCase String Method will return the result as Zero.

Java compareToIgnoreCase Method Example

In this Java program, we are going to use the Java string.compareToIgnoreCase to lexicographically compare the string and user-specified string data.

// Java String.compareToIgnoreCase example
package StringFunctions;

public class CompareToIgnoreCase {
	public static void main(String[] args) {
		String str = "Tutorial GateWay";
		String str1 = "TUTORIAL GATEWAY";
		String str2 = "Tutorial";
		String str3 = "TutorIAL GATEway WebSITe";
		
		int a = str.compareToIgnoreCase(str1);
		int b = str.compareToIgnoreCase(str2);
		int c = str.compareToIgnoreCase(str3);
		int d = str2.compareToIgnoreCase("TUTORIAL");
		int e = str2.compareToIgnoreCase("TutoRiAl");
		int f = str2.compareToIgnoreCase("Tutorial");

		System.out.println("Comparing the String str and str1 = " + a);
		System.out.println("Comparing the String str and str2 = " + b);
		System.out.println("Comparing the String str and str3 = " + c);
		System.out.println("Comparing the String str2 and Literal = " + d);
		System.out.println("Comparing the String str2 and Literal = " + e);
		System.out.println("Comparing the String str2 and Literal = " + f);
	}
}
Java compareToIgnoreCase 1

Within this compareToIgnoreCase example, we declared four String literals str, str1, str2, str3. And then assigned corresponding values using the following statement.

String str = "Tutorial GateWay";
String str1 = "TUTORIAL GATEWAY";
String str2 = "Tutorial";
String str3 = "TutorIAL GATEway WebSITe";

It will call the public int compareToIgnoreCase (String str) method to compare the string str with str1. From the above screenshot, observe that it is returning Zero because they both are lexicographical equal.

int a = str.compareToIgnoreCase(str1);

The following statement will compare the string str with str2. From the above screenshot, observe that it is returning 8 because str has extra 8 character values.

int b = str.compareToIgnoreCase(str2);

This Java compareToIgnoreCase statement will compare the string str with str3. From the above Java screenshot, observe that it is returning -8 because str3 has extra 8 character values compared to str.

int c = str.compareToIgnoreCase(str3);

Next, we assigned the string directly inside our String.compareToIgnoreCase method. Note, for the third statement, replace the compareToIgnoreCase method with the compareTo method. Because both the strings are in the same case. Lastly, we used the System.out.println statements to print the output.

int d = str2.compareToIgnoreCase("TUTORIAL");
int e = str2.compareToIgnoreCase("TutoRiAl");
int f = str2.compareToIgnoreCase("Tutorial");