Java 如何从句子中的arraylist中获取任何单词首次出现的索引
我想从这个句子中得到单词的索引。但在这里我不想检查一个特定的词。我有一个单词列表,我想从这个列表中找到句子中第一个出现的单词的索引。 我想让索引得到句子的子字符串,从结果索引开始Java 如何从句子中的arraylist中获取任何单词首次出现的索引,java,android,string,search,arraylist,Java,Android,String,Search,Arraylist,我想从这个句子中得到单词的索引。但在这里我不想检查一个特定的词。我有一个单词列表,我想从这个列表中找到句子中第一个出现的单词的索引。 我想让索引得到句子的子字符串,从结果索引开始 String sentence = "hii rahul ,nice to meet you .How are you?"; ArrayList search = new ArrayList(); search.add("are"); search.add("rahul"); search.add("meet"); f
String sentence = "hii rahul ,nice to meet you .How are you?";
ArrayList search = new ArrayList();
search.add("are");
search.add("rahul");
search.add("meet");
for(int i=0;i<search.size();i++)
{
if (sentence.contains(search.get(i))) {
System.out.println("I found the keyword");
} else {
System.out.println("not found");
}
我试着写一些代码,但不知道如何获取字符串rahul的索引
输入:
你好,拉胡尔,很高兴见到你。你好吗?
搜索词的数组列表:[满足,是,rahul]
预期产出:
索引为4,因为rahul在句子中排在第一位您可以使用该方法。但是请注意,索引从0开始,因此在您的示例中,输出为4。您可以使用String.indexOfString来确定子字符串的起始位置:
Integer lowestIndex = null;
for(String searchWord : search) {
int index = sentence.indexOf(searchWord);
// update the result if the searchWord occurs at a lower position
if (index >= 0 && (lowestIndex == null || lowestIndex > index)) {
lowestIndex = index;
}
}
}
if (lowestIndex == null) {
System.out.println("None of the keywords were found");
}
else {
System.out.printf("First keyword at %s%n", lowestIndex);
}
也许是这样的:
int firstIndex = Integer.MAX_VALUE;
for(String word : search) {
int foundIndex = sentence.indexOf(word);
if(foundIndex != -1 && foundIndex < firstIndex){
firstIndex = foundIndex;
}
}
if(firstIndex != Integer.MAX_VALUE){
System.out.println("Found index is: " + firstIndex);
} else{
System.out.println("None of the words were found in the sentence.");
}
如果找不到该单词,indexOf将返回-1。如果找到它,我们会将最低值保存在firstIndex变量中
如果要从列表开始,请执行以下操作:
List<String> keywords = Arrays.asList("meet","are","rahul");
String pattern = keywords.stream().collect(Collectors.joining("|", "(", ")"));
正则表达式搜索速度较慢,但可以添加单词边界\\bmeet | are | rahul,以便找不到软件。或者进行不区分大小写的搜索。您可能需要将字符串拆分为单词列表 如果您只是使用contains或indexOf,它可能给出错误的答案。例如
String search = "Doctor Smith went gardening and then went to the cinema on Tuesday";
List<String> words = Arrays.asList("then", "to", "went");
Rahul的指数5如何?此外,在您的代码中,您使用字符串“hey rahul,你好吗?”在输入示例中,您给出了“hii rahul,很高兴认识您。你好吗?”也要澄清这一点。我认为OP意味着索引应该是4,rahul在字符串hey rahul中的起始位置。。。。OP可能从1开始计数,而不是从0开始……因此OP基本上希望在搜索词的字符串中找到最先出现的起始位置。如果不输出索引,为什么期望索引作为输出?只有在找到或未找到搜索词时才输出。如果要匹配整个单词,可能无法使用包含。例如,如果你的句子是医生去商店,而searfch的一个术语是to,那么它会在Doctorsoftware这个词中选择to?那是哪里?@AdriaanKoster在搜索时也会找到are软件。
String search = "Doctor Smith went gardening and then went to the cinema on Tuesday";
List<String> words = Arrays.asList("then", "to", "went");
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class FindWord {
public static void main(String[] args) {
String search = "Doctor Smith went gardening then went to the cinema on Tuesday";
List<String> words = Arrays.asList("then", "to", "went");
int index = 0;
int result = -1;
String match = null;
StringTokenizer tokenizer = new StringTokenizer(search, " ", true);
while(result < 0 && tokenizer.hasMoreElements()) {
String next = tokenizer.nextToken();
if(words.contains(next)) {
result = index;
match = next;
} else {
index += next.length();
}
}
if(match == null) {
System.out.println("Not found.");
} else {
System.out.println("Found '" + match + "' at index: " + result);
}
}
}