How to make my string comparison case insensitive?

I created a Java program to compare two strings:

String s1 = "Hello"; String s2 = "hello"; if (s1.equals(s2)) { System.out.println("hai"); } else { System.out.println("welcome"); } 

"Welcome" is displayed. I understand that it is case sensitive. But my problem is that I want to compare two strings without case sensitivity. That is, I expect the result to be hai .

+81
java string comparison case-insensitive
Feb 08 '10 at 8:50
source share
12 answers
  • Best to use s1.equalsIgnoreCase(s2) : (see javadoc )
  • You can also convert them to upper / lower case and use s1.equals(s2)
+136
Feb 08 '10 at 8:52
source share
+35
Feb 08 '10 at 8:52
source share

You must use the compareToIgnoreCase method of the String object.

 int compareValue = str1.compareToIgnoreCase(str2); 

if (compareValue == 0) this means that str1 is equal to str2 .

+16
May 01 '12 at 5:09
source share

String.equalsIgnoreCase is the most practical choice for naive unsigned string comparisons.

However, it is good to know that this method does not perform full bending or decomposition, and therefore cannot perform a comparison without content, as specified in the Unicode standard. In fact, the JDK APIs do not provide access to information about the character data of the case folding, so this task is best delegated to a trusted and verified third-party library.

This is the ICU library, and here's how to implement a case-insensitive string comparison utility:

 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); 

A naive comparison with String.equalsIgnoreCase , or String.equals in strings with upper or lower case will fail even this simple test.

(Remember that the predefined fake flavor of the getNFKCCasefoldInstance case getNFKCCasefoldInstance not depend on the locale, and for Turkish locales, a little more work with UCharacter.foldCase .

+16
Jan 23 '16 at 16:56
source share
 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 he will output: hai

+8
Feb 13 '14 at 9:33
source share

In the standard Java API, you have:

 String.CASE_INSENSITIVE_ORDER 

Therefore, you do not need to rewrite the comparator if you must use rows with Sorted data structures.

 String s = "some text here"; s.equalsIgnoreCase("Some text here"); 

This is what you want for pure equality checks in your own code.

Just for more info on everything regarding string equality in Java. The hashCode () function of the java.lang.String class is case sensitive:

 public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; } 

So, if you want to use a Hashtable / HashMap with strings as keys and have keys such as "SomeKey", "SOMEKEY" and "somekey" are considered equal, you will have to wrap the string in another class (you cannot extend String, so like this is the final class). For example:

 private static class HashWrap { private final String value; private final int hash; public String get() { return value; } private HashWrap(String value) { this.value = value; String lc = value.toLowerCase(); this.hash = lc.hashCode(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o instanceof HashWrap) { HashWrap that = (HashWrap) o; return value.equalsIgnoreCase(that.value); } else { return false; } } @Override public int hashCode() { return this.hash; } } 

and then use it as such:

 HashMap<HashWrap, Object> map = new HashMap<HashWrap, Object>(); 
+3
Aug 21 '13 at 13:36 on
source share

Note that you can do null checks on them, as well as before executing your .equals or .equalsIgnoreCase.

The null String object cannot call the equals method.

t

 public boolean areStringsSame(String str1, String str2) { if (str1 == null && str2 == null) return true; if (str1 == null || str2 == null) return false; return str1.equalsIgnoreCase(str2); } 
+2
Apr 09 '13 at 3:57
source share
+1
Feb 08 '10 at 8:53
source share

You can use equalsIgnoreCase

+1
Feb 08 '10 at 8:53
source share

Details on the string can be found in String Class and String Tutorials

+1
Feb 08 2018-10-10T00
source share

To be null you can use

 org.apache.commons.lang.StringUtils.equalsIgnoreCase(String, String) 

or

 org.apache.commons.lang3.StringUtils.equalsIgnoreCase(CharSequence, CharSequence) 
0
Sep 12 '17 at 12:34 on
source share
 public boolean newEquals(String str1, String str2) { int len = str1.length(); int len1 = str2.length(); if(len==len1) { for(int i=0,j=0;i<str1.length();i++,j++) { if(str1.charAt(i)!=str2.charAt(j)) return false; }`enter code here` } return true; } 
-four
Nov 15 '13 at 5:28
source share



All Articles