Java compareToIgnoreCase

The Java compareToIgnoreCase method is one of the String Methods, which is to compare the string with a user-specified string lexicographically and ignore the case difference.

This article will show how to write compareToIgnoreCase with an example. The syntax of the String compareToIgnoreCase in Java Programming language is as shown below.

Java compareToIgnoreCase Method syntax

The following compareToIgnoreCase 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 method will return the result as a Negative Integer.
  • If the String_Object lexicographically follows the string Str, it will return the result as Positive Integer.
  • And if the String_Object and the string Str are equal, the String Method will return the result as Zero.

Example

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

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 Java compareToIgnoreCase example, we declared four String literals str, str1, str2, str3. And then assigned corresponding values using the first four statements.

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 compareToIgnoreCase statement will compare the string str with str3. From the above Java screenshot, observe that it is returning -8 because str3 has an extra 8 character value 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 this 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");