Java String equalsIgnoreCase()The Java String class equalsIgnoreCase() method compares the two given strings on the basis of the content of the string irrespective of the case (lower and upper) of the string. It is just like the equals() method but doesn't check the case sensitivity. If any character is not matched, it returns false, else returns true. SignatureParameterstr : another string i.e., compared with this string. ReturnsIt returns true if characters of both strings are equal, ignoring case otherwise false. Internal implementationIt is obvious from looking at the implementation that the equalsIgnoreCase() method is invoking the regionMatches() method. It makes the equalsIgnoreCase() method case-insensitive. The signature of the regionMatches() method is mentioned below. public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) The regionMatches() method parses five parameters. The first parameter ignoreCase is set to true in the above implementation. Thus, when the method is executed, it checks whether the ignoreCase flag is true or not. If yes, then one character each from both the strings is taken and then compared. If the comparison gives a false value, then both the character is converted to upper case and then checked if the comparison still gives a false value, then both the characters are converted to a lower case and then compared. If the comparison gives the true value, then both the strings have equal contents; otherwise, not. Code Snippet of the discussed comparison is mentioned below. One may argue that if we made a comparison after converting to uppercase, then why do we need an extra comparison by converting characters to the lowercase. The reason behind this is to provide to the requirement of Georgian alphabets. Conversion in uppercase does not work properly for the Georgian alphabets, as they have some strange rules about the case conversion. Therefore, one extra comparison, by converting characters to the lowercase, is required. Java String equalsIgnoreCase() Method ExampleFileName: EqualsIgnoreCaseExample.java Test it NowOutput: true true false Java String equalsIgnoreCase() Method Example 2Let's see an example where we are testing string equality among the strings. FileName: EqualsIgnoreCaseExample2.java Output: Mukesh kumar is present Next TopicJava String format() |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India