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");
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.
An array can be sorted in case-insensitive order using the java. util. Arrays. sort() method.
Both String#includes() and String#indexOf() are case sensitive.
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)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With