【问题标题】:how to detect if word is on the sentence - android如何检测单词是否在句子中 - android
【发布时间】:2014-10-23 14:13:39
【问题描述】:

我尝试在数组上创建一个搜索词,但我不知道如何检测该词是否在句子中。 我使用此链接上的代码。 To check if string contains particular word

这是我现在使用的代码。但它不起作用。

for(int x=0;x<=count;x++){
String sentence = name[x] ;
String search  = myEditText.getText().toString() ;
                        if (sentence.toLowerCase().indexOf(search.toLowerCase()) != -1 ) {
                         name[x] = name2;
                            schoolid[x] = id2;
}
}      

【问题讨论】:

  • "你好,这里是你通常的搜索方式".contains("你好")
  • @MLProgrammer-CiM contains(s) indexOf(s.toString()) &gt; -1;。 OP 代码应该可以正常工作吗?
  • 只是提到..这是一个Java问题而不是“android”

标签: android arrays search


【解决方案1】:

就像@MLProgrammer-CiM 说的: java.lang.String 类的公共方法:

boolean contains(CharSequence cs)

如果此字符串包含,则返回 true 来自给定 CharSequence 的字符。

这意味着如果搜索包含在句子中,则以下将返回true

sentence.contains(search);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多