Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array contains() without case sensitive lookup? [duplicate]

Tags:

Can I somehow tell the array.contains() method to not make the lookup case sensitive?

List<String> data = Arrays.asList(   "one", Two", "tHRee"); //lots of entries (100+)  data.contains("three"); 
like image 231
membersound Avatar asked Mar 07 '13 11:03

membersound


People also ask

How do you use includes in case insensitive?

To use JavaScript includes() in a case insensitive manner, we can convert all strings to the same case. const arr = ["foo", "bar", "bar"]; const lookup = "bAr"; console.

How do you make an array not case sensitive in Java?

An array can be sorted in case-insensitive order using the java. util. Arrays. sort() method.

Are .includes case sensitive?

Both String#includes() and String#indexOf() are case sensitive.


1 Answers

contains just check if an object is present in the List. So you can't do a case insensitive lookup here, because "three" is a different object than "Three".

A simple approach to solve this would be

public boolean containsCaseInsensitive(String s, List<String> l){      for (String string : l){         if (string.equalsIgnoreCase(s)){             return true;          }      }     return false;   } 

and then

containsCaseInsensitive("three", data); 

Java 8+ version:

public boolean containsCaseInsensitive(String s, List<String> l){         return l.stream().anyMatch(x -> x.equalsIgnoreCase(s));     } 
like image 58
Averroes Avatar answered Sep 18 '22 19:09

Averroes