Java String Method compareToIgnoreCase()

This method is used to compare two string with case ignorance and compare both string lexicographically. If both strings are same then it returns 0. If first string is greater then another then it returns positive integer, if second string is greater then first then it returns negative integer value.


public int compareToIgnoreCase(String str)


str – String value that will compared with string.


Integer value. if string is greater then passed str value then it returns positive integer if both strings are equal then it returns zero if passed str is greater then string then this method returns negative integer value.



In above program comparing same string is returning 0. In second println() statement A is lexicographically smaller then b so it is returning -1. In third println() statement C is lexicographically greater then b so it is returning 1 and so on.