The best way is to use str.equalsIgnoreCase("foo"). It's optimized specifically for this purpose.

You can also convert both strings to upper- or lowercase before comparing them with equals. This is a trick that's useful to remember for other languages which might not have an equivalent of equalsIgnoreCase.

str.toUpperCase().equals(str2.toUpperCase())

If you are using a non-Roman alphabet, take note of this part of the JavaDoc of equalsIgnoreCase which says

Note that this method does not take locale into account, and will result in unsatisfactory results for certain locales. The Collator class provides locale-sensitive comparison.

Answer from Michael Bavin on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_equalsignorecase.asp
Java String equalsIgnoreCase() Method
String myStr1 = "Hello"; String ...yStr1.equalsIgnoreCase(myStr3)); // false ... The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences....
🌐
W3Resource
w3resource.com › java-tutorial › string › string_equalsignorecase.php
Java String: equalsIgnoreCase Method - w3resource
Java String equalsIgnoreCase Method: The equalsIgnoreCase() Method is used to compare a specified String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding ...
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_equalsignorecase.htm
Java - String equalsIgnoreCase() Method
Following is the declaration for ... − This is the String to compare this String against. This method returns true if the argument is not null and it represents an equivalent String ignoring case, else false....
🌐
Scaler
scaler.com › home › topics › equalsignorecase() in java
equalsIgnoreCase() in Java - Scaler Topics
April 26, 2022 - The equalsIgnoreCase() method in Java compares the specified string with another string, ignoring lower and upper case differences. The method returns true if the strings are equal, and false if not.
🌐
Baeldung
baeldung.com › home › java › java string › java string equalsignorecase()
Java String equalsIgnoreCase() | Baeldung
January 8, 2024 - String lower = "equals ignore case"; String UPPER = "EQUALS IGNORE CASE"; assertThat(StringUtils.equalsIgnoreCase(lower, UPPER)).isTrue(); assertThat(StringUtils.equalsIgnoreCase(lower, null)).isFalse();
🌐
CodeGym
codegym.cc › java blog › strings in java › string equalsignorecase() method in java
String equalsIgnoreCase() Method in Java
February 13, 2025 - The equalsIgnoreCase() method in Java is used to compare two strings while ignoring the case differences (lower and upper). Like the equals method, it compares the contents of both strings. If the contents are...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-equalsignorecase-method-with-examples
Java String equalsIgnoreCase() Method - GeeksforGeeks
December 23, 2024 - In Java, equalsIgnoreCase() method of the String class compares two strings irrespective of the case (lower or upper) of the string. This method returns a boolean value, true if the argument is not null and represents an equivalent String ignoring ...
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › equalsIgnoreCase
Java String equalsIgnoreCase() - Compare Ignoring Case | Vultr Docs
December 18, 2024 - Use the equalsIgnoreCase() method to compare these two strings. ... String str1 = "java"; String str2 = "JAVA"; boolean result = str1.equalsIgnoreCase(str2); System.out.println(result); Explain Code
🌐
Programiz
programiz.com › java-programming › library › string › equalsignorecase
Java String equalsIgnoreCase()
str1 and str2 are equal if you do not consider case differences. Hence, str1.equalsIgnoreCase(str2) returns true. str1 and str3 are not equal. Hence, str1.equalsIgnoreCase(str3) and str3.equalsIgnoreCase(str1) returns false. class Main { public static void main(String[] args) { String str1 ...
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-equals-and-equalsignorecase-methods-example
Java String equals() and equalsIgnoreCase() Methods example
The method equalsIgnoreCase() ignores the case while comparing two strings. In the following example we compared the string “Apple” with the string “APPLE” and it returned true. public class JavaExample{ public static void main(String args[]){ String str1= new String("Apple"); String ...
🌐
How to do in Java
howtodoinjava.com › home › string › java string equalsignorecase()
Java String equalsIgnoreCase() with Examples - HowToDoInJava
January 6, 2023 - The Java String.equalsIgnoreCase() compares the current string with the specified string in a case-insensitive manner. Using equalsIgnoreCase(), two strings are considered equal if they are of the same length and corresponding characters in ...
🌐
Medium
medium.com › javarevisited › micro-optimizations-in-java-string-equalsignorecase-ea25dfb03f95
Micro optimizations in Java. String.equalsIgnoreCase() | by Dmytro Dumanskiy | Javarevisited | Medium
August 26, 2020 - For example, in case you have control over the data or the server API, you can do toLowerCase() on the UI or you can require the parameters of the API to be case sensitive. We do it a lot in Blynk · When you don’t have control over the input, choose equalsIgnoreCase() over equals(param.toLowerCase()). It doesn’t allocate unnecessary objects, has a fast path when the string length doesn't match and it’s still faster on Java 8
🌐
Javatpoint
javatpoint.com › java-string-equalsignorecase
Java String equalsIgnoreCase() method - javatpoint
Java String equalsIgnoreCase() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string equalsignorecase in java etc.
🌐
W3Schools
w3schools.com › java › ref_string_comparetoignorecase.asp
Java String compareToIgnoreCase() Method
String myStr1 = "HELLO"; String myStr2 = "hello"; System.out.println(myStr1.compareToIgnoreCase(myStr2)); ... The compareToIgnoreCase() method compares two strings lexicographically, ignoring lower case and upper case differences.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.equalsignorecase
String.EqualsIgnoreCase(String) Method (Java.Lang) | Microsoft Learn
Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true: <ul> <li> The two characters are the same (as compared by the == operator) <li> Calling Character.toLowerCase(Character.toUpperCase(char)) on each character produces the same result </ul> Note that this method does <em>not</em> take locale into account, and will result in unsatisfactory results for certain locales. The java.text.Collator class provides locale-sensitive comparison.
🌐
Codecademy
codecademy.com › docs › java › strings › .equalsignorecase()
Java | Strings | .equalsIgnoreCase() | Codecademy
February 23, 2023 - The .equalsIgnoreCase() method compares two strings ignoring lower/upper case differences and returns true if the two are the same. Otherwise, it returns false. ... Learn to code in Java — a robust programming language used to create software, ...
🌐
GeeksforGeeks
geeksforgeeks.org › equalsignorecase-in-java
equalsIgnoreCase() in Java - GeeksforGeeks
December 4, 2018 - This method returns true if the argument is not null and it represents an equivalent String ignoring case, else false. ... Input : str1 = "pAwAn"; str2 = "PAWan" str2.equalsIgnoreCase(str1); Output :true Input : str1 = "powAn"; str2 = "PAWan" ...