java String

String compareToIgnoreCase() in java Code With Examples

Table of Contents

Program

public class StringCompareToIgnoreCase
{
	public static void main(String args[])
	{
		String str1 = "Venkatesh From France";
		String str2 = "Muruga Balan From Chennai";
		String str3 = "I Am Here";
		int result = str1.compareToIgnoreCase(str2);
		System.out.println(result);
		result = str2.compareToIgnoreCase(str3);
		System.out.println(result);
		result = str3.compareToIgnoreCase(str1);
		System.out.println(result);
	}
}

Output

9
4
-13

Description

public int compareToIgnoreCase​(String str)

Compares two strings lexicographically, ignoring case differences. This method returns an integer whose sign is that of calling compareTo with normalized versions of the strings where case differences have been eliminated by calling Character.toLowerCase(Character.toUpperCase(character)) on each character.
Note that this method does not take locale into account, and will result in an unsatisfactory ordering for certain locales. The Collator class provides a locale-sensitive comparison.

Parameters:

str – the String to be compared.

Returns:

a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.

Since:

1.2