🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
String.compareToIgnoreCase() method in Java is used to compare two strings lexicographically, ignoring case differences. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
compareToIgnoreCaseMethod Syntax- Examples
- Comparing Equal Strings
- Comparing Different Strings
- Case Sensitivity Ignored
- Handling Null Values
- Conclusion
Introduction
The String.compareToIgnoreCase() method is a member of the String class in Java. It allows you to compare two strings lexicographically while ignoring differences in case. This method is particularly useful for sorting, searching, and comparing strings in a case-insensitive manner.
compareToIgnoreCase Method Syntax
The syntax for the compareToIgnoreCase method is as follows:
public int compareToIgnoreCase(String str)
- str: The string to be compared.
Examples
Comparing Equal Strings
The compareToIgnoreCase method returns 0 when the strings are equal, ignoring case.
Example
public class CompareToIgnoreCaseExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "hello";
int result = str1.compareToIgnoreCase(str2);
System.out.println("Comparison result: " + result);
}
}
Output:
Comparison result: 0
Comparing Different Strings
The compareToIgnoreCase method returns a negative integer if the first string is lexicographically less than the second string, and a positive integer if it is greater, ignoring case.
Example
public class CompareToIgnoreCaseExample {
public static void main(String[] args) {
String str1 = "apple";
String str2 = "Banana";
int result = str1.compareToIgnoreCase(str2);
System.out.println("Comparison result: " + result);
}
}
Output:
Comparison result: -1
Case Sensitivity Ignored
The compareToIgnoreCase method ignores case differences when comparing strings.
Example
public class CompareToIgnoreCaseExample {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "JAVA";
int result = str1.compareToIgnoreCase(str2);
System.out.println("Comparison result: " + result);
}
}
Output:
Comparison result: 0
Handling Null Values
The compareToIgnoreCase method does not handle null values and will throw a NullPointerException if the specified string is null.
Example
public class CompareToIgnoreCaseExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = null;
try {
int result = str1.compareToIgnoreCase(str2);
System.out.println("Comparison result: " + result);
} catch (NullPointerException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Error: Cannot invoke "String.compareToIgnoreCase(String)" because "str2" is null
To handle potential null values, you can implement a custom comparison logic.
Example (Safe Comparison)
public class CompareToIgnoreCaseExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = null;
int result = compareStringsIgnoreCaseSafe(str1, str2);
System.out.println("Comparison result: " + result);
}
public static int compareStringsIgnoreCaseSafe(String str1, String str2) {
if (str1 == null && str2 == null) {
return 0;
}
if (str1 == null) {
return -1;
}
if (str2 == null) {
return 1;
}
return str1.compareToIgnoreCase(str2);
}
}
Output:
Comparison result: 1
Conclusion
The String.compareToIgnoreCase() method in Java is used for comparing strings lexicographically while ignoring case differences. By understanding how to use this method, you can efficiently sort, search, and compare strings in a case-insensitive manner in your Java applications. Whether you are comparing equal strings, different strings, or handling potential null values, the compareToIgnoreCase method provides a reliable solution for these tasks.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment