如何从字符串 android 中查找特定单词

how to find particular word from string android

我想检查字符串中是否存在忽略大小写的特定单词。

我的字符串是 - "Hello world , Wassup? , good moRNING".

现在我想检查这个词是否 - "hello world"。

所以我尝试了以下方法:

String fullText = "Hello world , Wassup? , good moRNING";
String singleWord = "hello world";
boolean find;

 if (fullText.matches(singleWord)) {

    find = True;
}

我也试过 contains 但这不起作用。

我怎样才能找到这个特定的词?

您可以尝试将要搜索的句子和字符串都小写,例如

if (fullText.toLowerCase().matches(singleWord.toLowerCase())) {
    find = True;
}

问题是这样的:

Hello world不包含hello world 因为它有大写字母。

使用这个:

if (fullText.toLowerCase().matches(singleWord.toLowerCase())) {
  find = True;
}
find = fullText.toUpperCase().contains(singleWord.toUpperCase());

你可以试试这个:

String string = "Test, I am Adam";
// Anywhere in string
b = string.indexOf("I am") > 0;         // true if contains 

// Anywhere in string
b = string.matches("(?i).*i am.*");     // true if contains but ignore case

// Anywhere in string
b = string.contains("AA")  ; 

您可以将两个字符串都转换为普通大小写,然后使用 indexOf 或匹配,您需要使用 regex.

String fullText = "Hello world , Wassup? , good moRNING";
String singleWord = "hello world";
boolean find;



if (fullText.toLowerCase().indexOf(singleWord.toLowerCase()) > -1) {

    find = true;
}