In Java, you can use the equalsIgnoreCase()
method of the String
class to compare the contents of two String
objects for equality, ignoring case. The equalsIgnoreCase()
method is similar to the equals()
method, but it compares the contents of the String
objects in a case-insensitive manner.
Here's an example of how to use the equalsIgnoreCase()
method to compare two String
objects in Java:
public class Main { public static void main(String[] args) { String str1 = "Hello"; String str2 = "hello"; String str3 = "HELLO"; System.out.println(str1.equalsIgnoreCase(str2)); // true System.out.println(str1.equalsIgnoreCase(str3)); // true } }
In this example, three String
objects are created: str1
, str2
, and str3
. str1
and str2
are initialized with the same string literal in different cases, and str3
is initialized with a string literal in uppercase.
The equalsIgnoreCase()
method is called on str1
and passed str2
and str3
as arguments. The equalsIgnoreCase()
method compares the contents of the String
objects in a case-insensitive manner and returns true
if they are equal, and false
otherwise.
In this example, the equalsIgnoreCase()
method returns true
for both comparisons because the contents of the String
objects are equal, ignoring case.
You can use the equalsIgnoreCase()
method to compare the contents of two String
objects in a case-insensitive manner in Java. Just call the equalsIgnoreCase()
method on one of the String
objects and pass the other String
object as an argument.