How to Implement String Case Insensitive Compare? How would you implement a String Comparator used in String#compareToIgnoreCase ? I may convert all character to upper case then compare like the following does: int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; int min = Math . min ( n1 , n2 ) ; for ( int i = 0 ; i < min ; i ++ ) { char c1 = Character . toUpperCase ( s1 . charAt ( i ) ) ; char c2 = Character . toUpperCase ( s2 . charAt ( i ) ) ; if ( c1 != c2 ) { return c1 - c2 ; } } return n1 - n2 ; It seems work and we may also use toLowerCase to replace toUpperCase . But the implementation in JDK source code doesn’t agree: int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; int min = Math . min ( n1 , n2 ) ; for ( int i = 0 ; i < min ; i ++ ) { char c1 = s1 . charAt ( i ) ; char c2 = s2 . charAt ( i ) ; if ( c1 != c2 ) { c1 = Character . to...
Learn programming, still on the way