String’s compareToIgnoreCase method is similar to compareTo method. Only difference with compareTo is that it ignores cases.It also compares Strings lexigraphically. Both String get converted to unicode value and then compares.
When you run above program, you will get below output:
If you call str1. compareToIgnoreCase(str2)
then if it returns
positive number : str1 is greater than str2
0: str1 is equal to str2
negative number : str1 is smaller than str2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; public class StringCompareToIgnoreCaseMain { public static void main(String[] args) { String str1="Java2blog"; String str2="hello"; String str3="World"; String str4="JAVA2BLOG"; System.out.println("comparing java2blog with hello : "+str1.compareToIgnoreCase(str2)); System.out.println("comparing hello with world : "+str2.compareToIgnoreCase(str3)); System.out.println("comparing java2blog with java2blog : "+str1.compareToIgnoreCase(str4)); } } |
1 2 3 4 5 |
comparing java2blog with hello : 2 comparing hello with world : -15 comparing java2blog with java2blog : 0 |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.