简体   繁体   English

contains() 方法未按预期工作

[英]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.我正在为 android 构建一个语音助手,这里这个方法会一个一个地检索联系人姓名并将其与 SpeechToText 输入进行比较。

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.这里例如我发送“call karan”作为输入,同时调试名称“Karan”作为名称变量的值,但是在if语句中比较它时,没有任何反应,我的意思是下一条语句没有执行,什么可能是问题所在,我们将不胜感激。

您还需要将name变量转换为小写,因为String.contains()方法区分大小写,所以call karan不包含Karan

if you just want to compare 2 strings, no matter if they are lower or upper case you should use equalsIgnoreCase .如果您只想比较 2 个字符串,无论它们是小写还是大写,都应该使用equalsIgnoreCase in your case:在你的情况下:

if (text_received.equalsIgnoreCase(name))
{
   //...
}

EDIT: according to Bohuslav Burghard comment if you need to search a specific part of the string and make it ignore case, you can use regular expression with match function:编辑:根据Bohuslav Burghard的评论,如果您需要搜索字符串的特定部分并使其忽略大小写,您可以使用带有匹配功能的正则表达式:

if (text_received.matches("(?i:.*" + name + ".*)"))
{
    //...
}

i used this method and it worked well.我使用了这种方法,效果很好。 match() did not fullfiled my needs neither equal() nor equalToIgnore... match() 没有满足我的需要,既不是 equal() 也不是 equalToIgnore ...

public static boolean containsIgnoreCase(String src, String what) {
    final int length = what.length();
    if (length == 0)
        return true; // Empty string is contained

    final char firstLo = Character.toLowerCase(what.charAt(0));
    final char firstUp = Character.toUpperCase(what.charAt(0));

    for (int i = src.length() - length; i >= 0; i--) {
        // Quick check before calling the more expensive regionMatches() method:
        final char ch = src.charAt(i);
        if (ch != firstLo && ch != firstUp)
            continue;

        if (src.regionMatches(true, i, what, 0, length))
            return true;
    }

    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM