Java String compareToIgnoreCase() Method
What is String?
In Java, an string class is a series of characters that are interpreted literally by a script. The string can be either only letters or be letters and numbers in most programming languages. For example, “PrepInsta” is a string containing a sequence of characters ‘P’, ‘r’, ‘e’ , ‘p’ , ‘I’, ‘n’, ‘s’ , ‘t’, ‘a’
To know more about String Class in java read the complete article.
String compareToIgnoreCase() Method:
The main work of java string class compareToIgnoreCase() is to compare the string with the current string lexicographically ignoring case considerations. A negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String.
- The method compareToIgnoreCase() belongs to the String class that belong to java.lang package.
Note: The given string and the current string is compared on the basis of the Unicode value of each character present in the string.
2. The outcome is a Positive integer if the first string is lexicographically longer than the second string.
3. The result is zero if it is lexically equal to the second string.
If the specified object is null, a NullPointerException is thrown.
Syntax:
public int compareToIgnoreCase(String str)
Parameters:
str - the String to be compared.
Let’s look at a string-related Java program where the Java String compareTo()Ignorecase() method is used to perform an operation on the given string.
Examples : Comparing two given strings ignoring lower and upper case difference
public class Main { public static void main(String[] args) { String myStr1 = "PREPINSTA PRIME"; String myStr2 = "prepinsta prime"; System.out.println(myStr1.compareToIgnoreCase(myStr2)); } }
Output
0
Examples : Comparing three given strings with eachothers ignoring lower and upper case.
public class Main { //give some complex exaqmples of compareToIgnoreCase public static void main(String[] args) { String s1 = "Hello"; String s2 = "hello"; String s3 = "Hello"; String s4 = "Hello World"; String s5 = "Hello World"; String s6 = "Hello World"; String s7 = "Hello World"; String s8 = "Hello World"; String s9 = "Hello World"; String s10 = "Hello World"; System.out.println(s1.compareToIgnoreCase(s2)); System.out.println(s1.compareToIgnoreCase(s3)); System.out.println(s1.compareToIgnoreCase(s4)); System.out.println(s1.compareToIgnoreCase(s5)); System.out.println(s1.compareToIgnoreCase(s6)); System.out.println(s1.compareToIgnoreCase(s7)); System.out.println(s1.compareToIgnoreCase(s8)); System.out.println(s1.compareToIgnoreCase(s9)); System.out.println(s1.compareToIgnoreCase(s10)); } }
Output
0 0 -6 -6 -6 -6 -6 -6 -6
Examples : Comparing two given strings ignoring lower and upper case.
public class Main { public static void main(String[] args) { String str1 = "Hello PREPSTERS!"; String str2 = "hello prepsters!"; if(str1.compareToIgnoreCase(str2)==0){ System.out.println("str1 is equal to str2"); } else{ System.out.println("str1 is not equal to str2"); } } }
Output
str1 is equal to str2
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment