Jithin Varghese
Jithin Varghese

Reputation: 2228

how to make a string case insensitive

I have one edittext and listview having some values. While typing text in edittext the listview contents will change according to the values typed inside edittext.

Now the problem is, the string is in case sensitive. ie, If the original text is Apparel, then if we type apparel or appa the original text is not displaying.

I want to make the string search case insensitive.

My code is,

private List<SearchList> searchTerms(List<SearchList> search_list, String s) {
    List<SearchList> matches = new ArrayList<SearchList>();
    for (SearchList search_lists : search_list) {
        if (search_lists.search_term.contains(s)) {
            matches.add(search_lists);
        }
    }
    return matches;
}

Is there any way to achieve this. I have tried a lot.

Upvotes: 3

Views: 4179

Answers (3)

Let&#39;sRefactor
Let&#39;sRefactor

Reputation: 3346

string contains(); function is case sensitive. And from your question, I've noticed that your list contains items with Capital letters as well in it. So apply toLowerCase() to both side would cut it.

if (search_lists.search_term.toLowerCase().contains(s.toLowerCase())) 
{
    matches.add(search_lists);
}

Hope this helps.

Upvotes: 6

arifhasnat
arifhasnat

Reputation: 103

Just do as like and compare, Hope it will work :)

   EditText editText= (EditText) findViewById(R.id.editText);
   String editTextString=editText.getText().toString();
   editTextString.equalsIgnoreCase(yourCompare);

Upvotes: 0

Incerteza
Incerteza

Reputation: 34884

Try to lower() a string:

private List<SearchList> searchTerms(List<SearchList> search_list, String s) {
    List<SearchList> matches = new ArrayList<SearchList>();
    for (SearchList search_lists : search_list) {
        if (search_lists.search_term.contains(s.toLowerCase())) {
            matches.add(search_lists);
        }
    }
    return matches;
}

Upvotes: 2

Related Questions