Java String compareToIgnoreCase() Method

You are Here:

Java String compareToIgnoreCase()

The compareToIgnoreCase() method compares the characters of two strings.

The characters of the strings may be in lower case or upper case, this method doesn't discriminates between them, i.e, Both 'A' and 'a' are equal.

The compareToIgnoreCase() method starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.

Example

Java Compiler
public class myClass { public static void main(String[] args) { String str1 = "apple"; String str2 = "APPLE"; int res = str1.compareToIgnoreCase(str2); if(res == 0) System.out.print("str1 is equal to str2"); else if(res > 0) System.out.print("str1 is greater than str2"); else System.out.print("str1 is less than str2"); } }

Output

str1 is equal to str2

Syntax

public int compareToIgnoreCase(String str2)

Parameter Values

ValueTypeExplanation
str2RequiredSpecifies a string to be compared.

Return Value

This function returns the numeric difference between the Unicode values of non-matching characters.

ValueExplanation
+iveIf str1 is greater than str2
0If str1 is equal to str2
-iveIf str1 is less than str2

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.

We are working to cover every Single Concept in Java.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author