0

I can not figure out a way to perform case insensitive search on a list, and at the sae time not make it word specific. For example searching for "anim" would return the results from the list that have those characters in that sequence so if there is word "animal" that list entry will be returned. Same goes for something like "als ea" so if there is "animals eat grass" or something like that in a list, return that entry.

Currently my list looks like

List<Animal> animals = database.getAllAnimals();

Essentially gets all data from animals, I then want to filter this whole data (I understand it might not be most efficient way and can be done in SQL, but this is how I need to do it) with a search term, lets assume for now searchTerm = "abc" and then make animals list only contain entries that return positive search results.

Ilja
  • 44,142
  • 92
  • 275
  • 498

1 Answers1

1

Okay so i get your question a little bit more now.Run this code and see what it does.

public class TestCompare {
public static void main (String[]args) {

    String firstString = "hello";


    boolean containsMySearch;

     firstString = firstString.toUpperCase();
            containsMySearch = firstString.contains("llo");

            System.out.println("returns " + containsMySearch);

}
}

This prints returns false.But if you remove the line that says firstString = firstString.toUppercase(), it prints returns true.I do not know why yours returns nothing.But you need to check that you are comparing it using the right case.That is, if in your database you have Animal, a search with anim will return nothing because of the case sensitivity.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
  • got it working, so in order to do case insensitive search I'd just convert user input with list strings and set both to uppercase ? – Ilja Mar 23 '14 at 17:18
  • Exactly.To set a particular letter (in this case the first letter of `anim` to `Anim`) before comparing it with the "original", take a look at [this](http://stackoverflow.com/questions/5725892/how-to-capitalize-the-first-letter-of-word-in-a-string-using-java) tutorial. because setting the entire string to upper or lower case won't make sense. – Ojonugwa Jude Ochalifu Mar 23 '14 at 17:22