When utilizing Java, evaluating two strings is a frequent operation, particularly when processing user entries, database searches, or enacting business rules. Java offers various approaches to assess the equality of two strings.
In this article, we will explore how to compare two strings in Java employing the String.equalsIgnoreCase() method.
Contents:
- What is the equalsIgnoreCase() Method in Java?
- Example 1: Comparing two strings in Java
- Example 2: Comparing User Input Strings in Java
- Conclusion
What is the equalsIgnoreCase() Method in Java?
The equalsIgnoreCase() method in Java evaluates two strings without considering their case (whether uppercase or lowercase). It yields true if both strings hold identical values. This method is useful in contexts where case sensitivity is not a priority, such as validating user inputs or conducting searches.
Syntax:
boolean result = string1.equalsIgnoreCase(string2);
Parameters:
- string1 refers to the initial string for comparison.
- string2 denotes the second string being compared to string1.
- result is a boolean outcome that can either be true or false.
Return value: The function returns a boolean value (either true or false).
Example 1: Comparing two strings in Java
Let’s examine the example below that assesses two strings without considering their case (uppercase or lowercase).
Code:
Output:

Explanation: In the illustration above, str1 and str2 consist of the same content, with one in uppercase and the other in lowercase. The equalsIgnoreCase() method returns true for str1.equalsIgnoreCase(str2) since it disregards case discrepancies. However, str1 and str3 do not have equivalent values, thus yielding false.
Example 2: Comparing User Input Strings in Java
Let’s observe the following example that accepts two strings as input and verifies their equality.
Code:
class=”maineditor maineditor69624″>
Output:

Clarification: In the aforementioned code, we have gathered two input strings from the user and verified if they are identical or not. If both strings match, it outputs “The strings are equal (ignoring case)”, otherwise, it displays the message “The strings are not equal.”
Summary
In this blog, we have understood how to compare two strings without considering their case (whether lowercase or uppercase). The equalsIgnoreCase() method is useful for performing case-insensitive string comparisons, which is beneficial in situations such as user authentication and form validation.
If you aspire to become proficient in Java Programming, you may refer to our Java Course.
Additional Methods to Compare Strings in Java
- Utilizing == Operator
- Employing Objects.equals() Method
- Using String.compareTo() Method
- Using compareToIgnoreCase() Method
- Utilizing startsWith() Method
- Employing endsWith() Method
- Implementing User-Defined Function
The post Java String equalsIgnoreCase() Method appeared first on Intellipaat Blog.
```
