{"id":89295,"date":"2024-02-26T19:00:36","date_gmt":"2024-02-26T13:30:36","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89295"},"modified":"2024-02-26T19:00:36","modified_gmt":"2024-02-26T13:30:36","slug":"java-string-equalsignorecase-method","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/","title":{"rendered":"Java String equalsIgnoreCase() Method"},"content":{"rendered":"<p>Strings in Java are capable of performing a wide range of operations. The Strings are widely used to perform operations like comparison, access, substitution, search and removal. As strings are an object, comparing them differs from comparing the values for other basic data types.<\/p>\n<p>In order for relational operations to run, String Objects must have specific functions. There are many ways of comparing the strings on Java. The method for checking whether two strings are equal to each other by not looking at their cases will be discussed in this article.<\/p>\n<h2>Definition<\/h2>\n<p>The method is to compare that String with another string, ignoring the case considerations. If the two strings are of the same length and correspond to each other in a given character, they shall be regarded as an equal disregard case.<\/p>\n<h3>Parameter Values<\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">Parameter<\/span><\/td>\n<td><span style=\"font-weight: 400\">Description<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">anotherString<\/span><\/td>\n<td><span style=\"font-weight: 400\">A string, which represents a second string to be compared<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Technical Details<\/h3>\n<p>Returns a boolean value: True if the strings are identical and ignore case differences False if the string isn&#8217;t equal.<\/p>\n<h3>How to use equalsIgnoreCase in Java?<\/h3>\n<h4>Example 1: Java String equalsIgnoreCase()<\/h4>\n<ul>\n<li>If you take no account of the differences in cases, str1 and str2 are equal. Hence, str1. equalsIgnoreCase(str2) returns true.<\/li>\n<li>It is not the same between str1 and str3. Hence, str1. equalsIgnorecase.str3 and str3. equalsIgnoreCase(str1) returns false.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p>The syntax of this method is shown here.<\/p>\n<p>public boolean equalsIgnoreCase(String otherString)<\/p>\n<h4>Parameters:<\/h4>\n<p>Details of the parameters are given here.<br \/>\nanotherString &#8211; A string that compares this string to it.<\/p>\n<h4>Return value:<\/h4>\n<p>If the argument is not null and the strings are case insensitive or otherwise false, this method returns true.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Techvidvan{\n   public static void main(String args[]) {\n      String Str1 = new String(\"This is really not immutable!!\");\n      String Str2 = Str1;\n      String Str3 = new String(\"This is really not immutable!!\");\n      String Str4 = new String(\"This IS REALLY NOT IMMUTABLE!!\");\n      boolean retVal;\n      retVal = Str1.equals( Str2 );\n      System.out.println(\"Returned Value = \" + retVal );\n      retVal = Str1.equals( Str3 );\n      System.out.println(\"Returned Value = \" + retVal );\n      retVal = Str1.equalsIgnoreCase( Str4 );\n      System.out.println(\"Returned Value = \" + retVal );\n   }\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nReturned Value = true<br \/>\nReturned Value = true<br \/>\nReturned Value = true<\/p>\n<h3>Difference between equals() vs equalsIgnoreCase() in Java:<\/h3>\n<p>Check whether a pair of strings are equal by using the equals() function in Java. In Java, to check for an equal representation of two strings that ignore a case, use equalsIgnoreCase().<\/p>\n<p>So, let me give you two strings<\/p>\n<ul>\n<li>String one =&#8221;qwerty&#8221;;<\/li>\n<li>String two = &#8220;Qwerty&#8221;;<\/li>\n<\/ul>\n<p>The two of them are identical, but the case is different. When the method excludes a case, it would consider both of these strings to be equivalent by means of equalsIgnoreCase().<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Techvidvan{\n    public static void main(String[] args) {\n       String one = \"qwerty\";\n       String two = \"Qwerty\";\n       if(one.equalsIgnoreCase(two)) {\n          System.out.println(\"String one is equal to two (ignoring the case) i.e. one==two\");\n       }\nelse{\n          System.out.println(\"String one is not equal to String two (ignoring the case) i.e. one!=two\");\n       }\n       if(one.equals(two)) {\n          System.out.println(\"String one is equal to two i.e. one==two\");\n       }\nelse{\n          System.out.println(\"String one is not equal to String two i.e. one!=two\");\n       }\n    }\n}<\/pre>\n<p><strong>Use of equalsIgnoreCase():<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class EqualIgnore{\n    public static void main(String[] args) \n    {\n        String str1 = \"HellO WORld\";\n        String str2 = \"hello world\";\n        String str3 = \"HELLO WORLD\";\n        String str4 = \"hello\";\n        Boolean techvidvan;\n        techvidvan = str1.equalsIgnoreCase(str2);\n        System.out.println(techvidvan);\n        techvidvan = str1.equalsIgnoreCase(str3);\n        System.out.println(techvidvan);\n        techvidvan= str2.equalsIgnoreCase(str4);\n        System.out.println(techvidvan);\n    }\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\ntrue<br \/>\ntrue<br \/>\nfalse<\/p>\n<h3>Explanation:<\/h3>\n<p>The code shown above defines four different strings &#8211; str1, str2, str3 and str4. Comparing str1, str2 and str3 with the equaleIgnoreCase() method returns true as they read the same word after ignoring the case of the individual letters.<\/p>\n<p>The fourth string is entirely different from the other three strings as it lacks a few characters; hence, the use of equalsIgnoreCase on str4 and any of the three other strings results in a false.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/10\/equalsIgnoreCase-explanation-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-89300 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/10\/equalsIgnoreCase-explanation-1.webp\" alt=\"equalsIgnoreCase() explanation\" width=\"500\" height=\"266\" \/><\/a><\/p>\n<h3>Exceptions from Java equalsIgnoreCase():<\/h3>\n<p>Let&#8217;s look at the code snippet (string1.equalsIgnoreCase(string2)) to understand the exceptions. <strong>The following cases may occur:<\/strong><\/p>\n<ul>\n<li>If the input string i.e. string2 is null, the method will return false, it will not throw any exceptions.<\/li>\n<li>If string1 is null, the method throws an exception i.e. NullPointerException.<\/li>\n<\/ul>\n<h3>Internal implementation:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public boolean equalsIgnoreCase(String anotherString) {    \n       return (this == anotherString) ? true    \n               : (anotherString != null)    \n               &amp;&amp; (anotherString.value.length == value.length)    \n               &amp;&amp; regionMatches(true, 0, anotherString, 0, value.length);    \n   }<\/pre>\n<p>Looking at the implementation, it is obvious that the equalsIgnoreCase() method calls the regionMatches() method. This causes the equalsIgnoreCase() method to be case insensitive. The regionMatches() method signature is shown below.<\/p>\n<p>public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)<\/p>\n<p>The regionMatches() method parses five parameters. The first ignoreCase parameter is set to true in the above implementation. So when the method is called, it checks whether the ignoreCase flag is true or not. If so, then one character each from both strings is taken and compared.<\/p>\n<p>If the comparison returns false, then both characters are converted to uppercase and then checked to see if the comparison still returns false, then both characters are converted to lowercase and then compared. If the comparison returns a true value, then both strings have the same content; otherwise not.<\/p>\n<h3>Conclusion<\/h3>\n<p>In this article, we have extensively discussed the equalsIgnoreCase() function in Java. We briefly discussed the use of the equalsIgnoreCase() method and then moved on to see how using this method can help reduce a significant amount of code in a program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Strings in Java are capable of performing a wide range of operations. The Strings are widely used to perform operations like comparison, access, substitution, search and removal. As strings are an object, comparing them&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":89302,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5371,296,5372,5373,5374,327,250,5375,5376],"class_list":["post-89295","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-equalsignorecase","tag-java","tag-java-equalsignorecase","tag-java-string-equalsignorecase-method","tag-java-string-equalsignorecase-method-with-examples","tag-java-tutorials","tag-learn-java","tag-string-equalsignorecase-method","tag-string-equalsignorecase-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String equalsIgnoreCase() Method - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java String equalsIgnoreCase() method is used to compare the String with another string, ignoring the case considerations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String equalsIgnoreCase() Method - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java String equalsIgnoreCase() method is used to compare the String with another string, ignoring the case considerations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-26T13:30:36+00:00\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String equalsIgnoreCase() Method - TechVidvan","description":"Java String equalsIgnoreCase() method is used to compare the String with another string, ignoring the case considerations.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String equalsIgnoreCase() Method - TechVidvan","og_description":"Java String equalsIgnoreCase() method is used to compare the String with another string, ignoring the case considerations.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-02-26T13:30:36+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java String equalsIgnoreCase() Method","datePublished":"2024-02-26T13:30:36+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/"},"wordCount":695,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/#primaryimage"},"thumbnailUrl":"","keywords":["equalsIgnorecase()","java","java equalsIgnorecase()","java string equalsIgnoreCase() method","java string equalsIgnoreCase() method with examples","java tutorials","Learn Java","string equalsIgnoreCase() method","string equalsIgnoreCase() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/","url":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/","name":"Java String equalsIgnoreCase() Method - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/#primaryimage"},"thumbnailUrl":"","datePublished":"2024-02-26T13:30:36+00:00","description":"Java String equalsIgnoreCase() method is used to compare the String with another string, ignoring the case considerations.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-equalsignorecase-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String equalsIgnoreCase() Method"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89295"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89295\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}