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.
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).
function closeoutput65461() {
jQuery(“.maineditor65461 .code-editor-output”).hide();
}
// Attach event listeners to the buttons
document.getElementById(“copyBtn65461”).addEventListener(“click”, copyCodeToClipboard65461);
document.getElementById(“runBtn65461”).addEventListener(“click”, runCode65461);
document.getElementById(“closeoutputBtn65461”).addEventListener(“click”, closeoutput65461);
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:
“`html
class=”maineditor maineditor69624″>
Java
Code Copied!
var isMobile = window.innerWidth “);
editor69624.setValue(decodedContent); // Set the default text
editor69624.clearSelection();
editor69624.setOptions({
maxLines: Infinity
});
function decodeHTML69624(input) {
var doc = new DOMParser().parseFromString(input, “text/html”);
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard69624() {
const code = editor69624.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert(“Code copied to clipboard!”);
function closeoutput69624() {
var code = editor69624.getSession().getValue();
jQuery(".maineditor69624 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn69624").addEventListener("click", copyCodeToClipboard69624);
document.getElementById("runBtn69624").addEventListener("click", runCode69624);
document.getElementById("closeoutputBtn69624").addEventListener("click", closeoutput69624);
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.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.