Search code examples
javaandroidstringcontains

contains() method not working as expected


I am building a voice assistant for android, here this method retrieves contact names one by one and compares it to to SpeechToText input.

I am successfully getting the contact names, but when I am comparing it with my input text, nothing is happening.

Here is the code

private void callFromContact(String text_received, int index){

    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext())
    {
        String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        if(text_received.toLowerCase().contains(name)){
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contactNames.add(name);
            contactNumbers.add(phoneNumber);
        }
    }

Here for example I sending "call karan" as input, while debugging the name "Karan" comes up as the value of name variable, but when comparing it in if-statement, nothing happens, I mean the next statement is not executed, what might be the problem, help would be appreciated.


Solution

  • You need to convert the name variable to lowercase too, because String.contains() method is case sensitive, so call karan doesn't contain Karan.