java-string-equalsignorecase()-method

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?

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:

Java

Code Copied!

var isMobile = window.innerWidth “);

editor65461.setValue(decodedContent); editor65461.clearSelection();

editor65461.setOptions({ maxLines: Infinity });

function decodeHTML65461(input) { var doc = new DOMParser().parseFromString(input, “text/html”); return doc.documentElement.textContent; }

// Function to copy code to clipboard function copyCodeToClipboard65461() { const code = editor65461.getValue(); navigator.clipboard.writeText(code).then(() => { jQuery(“.maineditor65461 .copymessage”).show(); setTimeout(function() { jQuery(“.maineditor65461 .copymessage”).hide(); }, 2000); }).catch(err => { console.error(“Error copying code: “, err); }); }

function runCode65461() { var code = editor65461.getSession().getValue(); jQuery(“#runBtn65461 i.run-code”).show(); jQuery(“.output-tab”).click();

jQuery.ajax({ url: “https://intellipaat.com/blog/wp-admin/admin-ajax.php”, type: “post”,

data: { language: “java”, code: code, cmd_line_args: “”, variablenames: “”, action: “compilerajax” }, success: function(response) { var myArray = response.split(“~”); var data = myArray[1]; jQuery(“.output65461”).html(“

"+data+"

“); jQuery(“.maineditor65461 .code-editor-output”).show(); jQuery(“#runBtn65461 i.run-code”).hide(); } }); }

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:

Comparing two strings in Java 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!”);

jQuery(“.maineditor69624 .copymessage”).show(); setTimeout(function() { jQuery(“.maineditor69624 .copymessage”).hide(); }, 2000); }).catch(err => { console.error(“Error while copying code: “, err); }); }

function runCode69624() {

var code = editor69624.getSession().getValue();

jQuery(“#runBtn69624 i.run-code”).show(); jQuery(“.output-tab”).click();

jQuery.ajax({ url: “https://intellipaat.com/blog/wp-admin/admin-ajax.php”, type: “post”,

data: { language: “java”, code: code, cmd_line_args: “”, variablenames: “”, action:”compilerajax” }, success: function(response) { var myArray = response.split(“~”); var data = myArray[1];

jQuery(“.output69624”).html(“

"+data+"");
									jQuery(".maineditor69624 .code-editor-output").show();
									jQuery("#runBtn69624 i.run-code").hide();

} })

}

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:

Comparing User Input Strings in Java 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.

“`


Leave a Reply

Your email address will not be published. Required fields are marked *

Share This