Java String compareToIgnoreCase example
String class compareToIgnoreCase method example
String class compareToIgnoreCase method example:- This example demonstrates the working compareToIgnoreCase method. this method like compareTo also generates the bases of number line i.e. negative, zero and positive numbers.
Syntax:-compareToIgnoreCase(String str)
Here is the code:-
/**
* @(#) CompareToIgnoreCaseString.java
* CompareToIgnoreCaseString class demonstrates the working of compareToIgnoreCase() method of String class of lang package
* @version 14-May-2008
* @author Rose India Team
*/
public class CompareToIgnoreCaseString {
public static void main(String args[]) {
String str = "i will rule this entire world", str1 = str.toUpperCase(), str2 = str
.toLowerCase(), str3 = str.trim(), str4 = str.substring(8), str5 = str , str6 = "1234567", str7 = "12345";
Integer in = new Integer(0);
// compareToIgnoreCase method check the String character values ignoring
// case senstivity case
// below in all three println method execution method returns zero(0),
// which shows method declares the Strings concerned as equal
if (str.compareToIgnoreCase(str1) == in) {
System.out.println("(" + str.compareToIgnoreCase(str1) + ")"
+ " ----Strings can be said as equal");
} else {
System.out.println("Strings are not equal");
}
if (str.compareToIgnoreCase(str2) == in) {
System.out.println("("
+ str.compareToIgnoreCase(str2)
+ ")"
+ " ----Strings str and str2 are equal according to this method ");
} else {
System.out.println("Strings are not equal");
}
if (str.compareToIgnoreCase(str3) == in) {
System.out.println("(" + str.compareToIgnoreCase(str3) + ")"
+ " ----Strings str and str3 can be said as equal");
} else {
System.out.println("--Strings str and str2 are not equal");
}
// method here returns a negative value that states that method has
// declared the String chosen be as unequal
if (str.compareToIgnoreCase(str4) == in) {
System.out.println(str.compareToIgnoreCase(str3)
+ " ----Strings str and str3 can be said as equal");
} else {
System.out.println("("
+ str.compareToIgnoreCase(str4)
+ ")"
+ " ----Strings str and str4 are not equal as in their case method returns a negative value ");
}
int prabhu = str5.codePointAt(5), heram = str.codePointAt(5);
str5 = Integer.toString(prabhu);
str = Integer.toString(heram);
// again here method returns zero(0), this means Strings under process
// contain the same values
if (str.compareToIgnoreCase(str5) == in) {
System.out.println(str.compareToIgnoreCase(str5)
+ " ----Strings str and str5 can be said as equal");
} else {
System.out.println("("
+ str.compareToIgnoreCase(str5)
+ ")"
+ " ----Strings str and str5 are not equal as in their case method returns a negative value ");
}
// here the method compareToIgnoreCase returns a positive number that
// shows that method get Strings value not equal but to be following
// each other
System.out.println(str6.compareToIgnoreCase(str7)
+ " ----Strings str6 and str7 have values following each other but not exactly same, therefore method returns a positive number for these String values");
}
} |
Output of the program:-
(0) ----Strings can be said as equal
(0) ----Strings str and str2 are equal according to this method
(0) ----Strings str and str3 can be said as equal
(-12) ----Strings str and str4 are not equal as in their case method returns a negative value
0 ----Strings str and str5 can be said as equal
2 ----Strings str6 and str7 have values following each other but not exactly same, therefore method returns a positive number for these String values
|
|