Case insensitive list Java

Java String equalsIgnoreCase[] method is used to compare a string with the method argument object, ignoring case considerations.

In equalsIgnoreCase[] method, two strings are considered equal if they are of the same length and corresponding characters in the two strings are equal ignoring case.

1. Java String equalsIgnoreCase[] method

/** * @param anObject - The object to compare * @return true - if the non-null argument string represents the same sequence of characters to this string * false - in all other cases */ public boolean equalsIgnoreCase[String anotherString] { return [this == anotherString] ? true : [anotherString != null] && [anotherString.value.length == value.length] && regionMatches[true, 0, anotherString, 0, value.length]; }
  1. Java equalsIgnoreCase[] method is used to check equal strings in case-insensitive manner.
  2. Do not use '==' operator. It checks the object references, which is not not desirable in most cases.
  3. Passing null to method is allowed. It will return false.

2. Java String equalsIgnoreCase[] example

Java program to check if two strings are equal [case-insensitive]. Notice that equals[] and equalsIgnoreCase[] methods behave in same way, except later is case-insensitive.

public static void main[String[] args] { String blogName = "howtodoinjava.com"; String authorName = "Lokesh gupta"; //1 - case-insensitive comparison isEqualString = blogName.equalsIgnoreCase["HOWTODOINJAVA.COM"]; //true //2 - case-insensitive comparison isEqualString = blogName.equalsIgnoreCase["howtodoinjava.com"]; //true //3 - check two strings for same character sequence ignoring case boolean isEqualString = blogName.equalsIgnoreCase[authorName]; //false //4 - null is allowed isEqualString = blogName.equalsIgnoreCase[null]; //false } }

3. Java String equals[] example

Java program to check if two strings are equal [case-sensitive] using equals[] method.

public class Main { public static void main[String[] args] { String blogName = "howtodoinjava.com"; String authorName = "Lokesh gupta"; //1 - check two strings for same character sequence boolean isEqualString = blogName.equals[authorName]; //false //2 isEqualString = blogName.equals["howtodoinjava.com"]; //true //3 isEqualString = blogName.equals[null]; //false //4 - case-sensitive isEqualString = blogName.equals["HOWTODOINJAVA.COM"]; //false } }

4. Difference between equals and equalsIgnoreCase

Clearly, the difference between equals and equalsIgnoreCase in Java is case-sensitity while performing the string comparisons.

  1. equals[] method does case-sensitive comparison.
  2. equalsIgnoreCase[] method does case-insensitive comparison.

Happy Learning !!

Reference: String Java Doc

Was this post helpful?

Let us know if you liked the post. Thats the only way we can improve.
Yes
No

Further Reading:

Java String equals[] method Java compare strings
Java String compareToIgnoreCase[] method example
Jersey Case-insensitive @Path URLs

Video liên quan

Chủ Đề