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));
}
Answer from Averroes on Stack Overflowcontains 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));
}
it will require you to write your own contains() method.
pointers for custom contains():
- iterate over all the elements in the list.
- compare the parameter string with all the values in
to make comparison case-insensitive be sure to apply any of toLowerCase or toUpperCase on BOTH parameter and the current element.
List list; public boolean contains(String str){
for(int i=0;i<list.size();i++){ if(list.elementAt(i).toLowerCase().Equals(str.toLowerCase())) return true; } return false; }
java - Option to ignore case with .contains method? - Stack Overflow
java - Checking if an ArrayList contains a certain String while being case insensitive - Stack Overflow
java - ArrayList contains case sensitivity - Stack Overflow
java - arraylist check if element exits here ignore the case - Stack Overflow
import java.util.ArrayList;
public class WordsCounter {
public static int occurences(String string, ArrayList<String> words){
int number = 0;
for(int i = 0; i < words.size(); i++){
if(words.get(i).equalsIgnoreCase(string)){
number++;
}
}
return number;
}
}I want to make a method that takes a string and a list of words as parameters, and returns how many of the words in the list could be found in the string at least once. It doesn't have to be the word exactly, just that a part of the string matches, like such:
If the String was "Sunshine is nice", and the list elements were "shine" and "nice", it would return 2.
I can't get it to work with the ignore of case, the program doesn't ignore case despite the method I used. I looked up String methods and equalsIgnoreCase seemed to be a good one but it does not work. I don't know if it's because it can't be applied to list elements (although they are Strings). Do any of you know how I should think regarding this?
If you're using Java 8
List<String> list = new ArrayList<>();
boolean containsSearchStr = list.stream().anyMatch("search_value"::equalsIgnoreCase);
I'm guessing you mean ignoring case when searching in a string?
I don't know any, but you could try to convert the string to search into either to lower or to upper case, then search.
// s is the String to search into, and seq the sequence you are searching for.
bool doesContain = s.toLowerCase().contains(seq);
Edit: As Ryan Schipper suggested, you can also (and probably would be better off) do seq.toLowerCase(), depending on your situation.
You will need to iterate over the list and check each element. This is what is happening in the contains method. Since you are wanting to use the equalsIgnoreCase method instead of the equals method for the String elements you are checking, you will need to do it explicitly. That can either be with a for-each loop or with a Stream (example below is with a Stream).
private final List<String> list = new ArrayList<>();
public void addIfNotPresent(String str) {
if (list.stream().noneMatch(s -> s.equalsIgnoreCase(str))) {
list.add(str);
}
}
If you are using Java7, simply override the contains() method,
public class CastInsensitiveList extends ArrayList<String> {
@Override
public boolean contains(Object obj) {
String object = (String)obj;
for (String string : this) {
if (object.equalsIgnoreCase(string)) {
return true;
}
}
return false;
}
}
If you are using Java 8.0, using streaming API,
List<String> arrayList = new ArrayList<>();
arrayList.stream().anyMatch(string::equalsIgnoreCase);
You can use this exactly like you'd use any other ArrayList. You can pass this List out to other code, and external code won't have to understand any string wrapper classes.
public class CustomStringList3 extends ArrayList<String> {
@Override
public boolean contains(Object o) {
String paramStr = (String)o;
for (String s : this) {
if (paramStr.equalsIgnoreCase(s)) return true;
}
return false;
}
}
In Java8, using anyMatch
List<String> list = Arrays.asList("XYZ", "ABC");
String matchingText = "xYz";
boolean isMatched = list.stream().anyMatch(matchingText::equalsIgnoreCase);
You could put all your strings in a TreeSet with a custom Comparator that ignores the case:
List<String> list = Arrays.asList("Chicken", "Duck");
Set<String> set= new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
set.addAll(list);
System.out.println(set.contains("Chicken")); //true
System.out.println(set.contains("chicken")); //true
For more complex strategies / locale, you can also use a Collator:
final Collator ignoreCase = Collator.getInstance();
ignoreCase.setStrength(Collator.SECONDARY);
List<String> list = Arrays.asList("Chicken", "Duck");
Set<String> set= new TreeSet<String>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return ignoreCase.compare(o1, o2);
}
});
set.addAll(list);
System.out.println(set.contains("Chicken"));
System.out.println(set.contains("chicken"));
Looping through the elements is what contains does for you so if you don't want to use a loop, its best not to use an ArrayList.
Without using a different collection your best option is to use
FOUND: {
for(String item: arrFoodItems) {
if (item.equalsIgnoreCase(string)) {
System.out.println(string+" already available in the List.");
break FOUND;
}
}
System.out.println(string + " not available");
}
You can use
org.apache.commons.lang3.StringUtils.containsIgnoreCase(CharSequence str,
CharSequence searchStr);
Checks if CharSequence contains a search CharSequence irrespective of case, handling null. Case-insensitivity is defined as by String.equalsIgnoreCase(String).
A null CharSequence will return false.
This one will be better than regex as regex is always expensive in terms of performance.
For official doc, refer to : StringUtils.containsIgnoreCase
Update :
If you are among the ones who
- don't want to use Apache commons library
- don't want to go with the expensive
regex/Patternbased solutions, - don't want to create additional string object by using
toLowerCase,
you can implement your own custom containsIgnoreCase using java.lang.String.regionMatches
public boolean regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len)
ignoreCase : if true, ignores case when comparing characters.
public static boolean containsIgnoreCase(String str, String searchStr) {
if(str == null || searchStr == null) return false;
final int length = searchStr.length();
if (length == 0)
return true;
for (int i = str.length() - length; i >= 0; i--) {
if (str.regionMatches(true, i, searchStr, 0, length))
return true;
}
return false;
}
If you won't go with regex:
"ABCDEFGHIJKLMNOP".toLowerCase().contains("gHi".toLowerCase())