How do I make my string comparison case-insensitive?

303,552

Solution 1

The best way is to use str.equalsIgnoreCase("foo"). It's optimized specifically for this purpose.

You can also convert both strings to upper- or lowercase before comparing them with equals. This is a trick that's useful to remember for other languages which might not have an equivalent of equalsIgnoreCase.

str.toUpperCase().equals(str2.toUpperCase())

If you are using a non-Roman alphabet, take note of this part of the JavaDoc of equalsIgnoreCase which says

Note that this method does not take locale into account, and will result in unsatisfactory results for certain locales. The Collator class provides locale-sensitive comparison.

Solution 2

Use String.equalsIgnoreCase().

Use the Java API reference to find answers like these:

https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String)

https://docs.oracle.com/javase/1.5.0/docs/api/

Solution 3

String.equalsIgnoreCase is the most practical choice for naive case-insensitive string comparison.

However, it is good to be aware that this method does neither do full case folding nor decomposition and so cannot perform caseless matching as specified in the Unicode standard. In fact, the JDK APIs do not provide access to information about case folding character data, so this job is best delegated to a tried and tested third-party library.

That library is ICU, and here is how one could implement a utility for case-insensitive string comparison:

import com.ibm.icu.text.Normalizer2;

// ...

public static boolean equalsIgnoreCase(CharSequence s, CharSequence t) {
    Normalizer2 normalizer = Normalizer2.getNFKCCasefoldInstance();
    return normalizer.normalize(s).equals(normalizer.normalize(t));
}
    String brook = "flu\u0308ßchen";
    String BROOK = "FLÜSSCHEN";

    assert equalsIgnoreCase(brook, BROOK);

Naive comparison with String.equalsIgnoreCase, or String.equals on upper- or lowercased strings will fail even this simple test.

(Do note though that the predefined case folding flavour getNFKCCasefoldInstance is locale-independent; for Turkish locales a little more work involving UCharacter.foldCase may be necessary.)

Solution 4

You have to use the compareToIgnoreCase method of the String object.

int compareValue = str1.compareToIgnoreCase(str2);

if (compareValue == 0) it means str1 equals str2.

Solution 5

import java.lang.String; //contains equalsIgnoreCase()
/*
*
*/
String s1 = "Hello";
String s2 = "hello";

if (s1.equalsIgnoreCase(s2)) {
System.out.println("hai");
} else {
System.out.println("welcome");
}

Now it will output : hai

Share:
303,552

Related videos on Youtube

user268018
Author by

user268018

Updated on July 08, 2022

Comments

  • user268018
    user268018 almost 2 years

    I created a Java program to compare two strings:

    String str = "Hello";
    
    if (str.equals("hello")) {
        System.out.println("match");
    } else {
        System.out.println("no match");
    }
    

    It's case-sensitive. How can I change it so that it's not?

    • fastcodejava
      fastcodejava about 14 years
      If you know it is case sensitive, you could convert both to lowercase or uppercase before comparing.
    • H2ONaCl
      H2ONaCl over 7 years
      if you use s1.equalsIgnoreCase(s2) you might fail to do it everywhere it needs to be done. I suggest that you find where the string comes from -- a file or database or user input perhaps -- and convert to either uppercase (or lowercase) and continue to use .equals for the comparison.
    • Ohad Schneider
      Ohad Schneider over 6 years
      Don't convert to lower/uppercase (as suggested by the comments above), use the accepted equalsIgnoreCase approach. Read up on the Turkish I problem and similar Unicode issues for the rationale.
    • Paul Rooney
      Paul Rooney over 6 years
      You should consider marking an answer as accepted. I'd suggest the first one that answered with equalsIgnoreCase.
    • Hakanai
      Hakanai over 5 years
      @OhadSchneider equalsIgnoreCase returns the wrong value for Turkish anyway, because it returns true for comparing "i" and "I", even though it should return false. So I suspect that if you want to take locales into account, a Collator is actually the way to go.
    • Ohad Schneider
      Ohad Schneider over 5 years
      @Trejkaz fair enough, it looks like equalsIgnoreCase is equivalent to toLowerCase / toUpperCase comparison anyway (docs.oracle.com/javase/9/docs/api/java/lang/…). Looks like .NET did it better with the StringComparison enum (docs.microsoft.com/en-us/dotnet/api/…).
    • Hakanai
      Hakanai over 5 years
      @OhadSchneider I wonder. It says that doing it per-character produces the same result, but doing toLowerCase / toUpperCase on the whole string and doing it per-character give two different results as well.
    • Orion
      Orion over 5 years
      Side note. Your misspelling hi. Just saying. It's really spelled hi.
  • jarnbjo
    jarnbjo about 14 years
    Just be aware that the two solutions are not necessarily identical for all locales. String#equalsIgnoreCase is not using locale specific casing rules, while String#toLowerCase and #toUpperCase do.
  • towi
    towi almost 11 years
    @jarnbjo Can you give an example where for that difference?
  • jarnbjo
    jarnbjo almost 11 years
    Locale specific case rules are at least implemented for Turkish and German. Turkish treat I with and without dot as two different letters, creating the lower/upper case pairs iİ and ıI while other languages treat iI as a pair and do not use the letters ı and İ. In German, the lower case ß is capitalized as "SS".
  • LuckyMe
    LuckyMe almost 11 years
    Note: second two statements can be combined to produce same result like this: if (str1 == null || str2 == null) return false;.
  • VeenarM
    VeenarM almost 11 years
    Modified code to be cleaner as per above comment - was long day :)
  • Barney
    Barney about 10 years
    You could also change the first line to if (str1 == str2) return true; which both caters for nulls and also shortcuts the case where the two string references refer to the same string object.