[英]how to get the index for first occurence of any word from arraylist in sentence
我想从句子中得到单词的索引。 但是在这里我不想检查一个特定的单词。 我有单词列表,我想从列表中获得该单词中第一个出现的索引。
我希望索引从生成的索引开始获取句子的子字符串。
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"
的索引。
输入:
句子: hii rahul ,nice to meet you .How are you?
ArraySearched单词列表: ["meet","are","rahul"]
预期输出:索引为4(因为rahul
在句子中排在第一位)
您可以使用String.indexOf(String)
确定子字符串的起始位置:
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);
}
Matcher m = Pattern.compile("(meet|are|rahul)").matcher(searchText);
if (m.find()) {
System.out.printf("Found '%s' at position %d%n",
m.group(), m.start());
}
如果要以列表开头:
List<String> keywords = Arrays.asList("meet","are","rahul");
String pattern = keywords.stream().collect(Collectors.joining("|", "(", ")"));
正则表达式搜索的速度较慢,但是可以添加单词边界\\\\b(meet|are|rahul)
因此找不到“软件”。 或执行不区分大小写的搜索。
您可以使用String.indexOf方法。 但是请注意,索引从0开始,因此在您的示例中输出为4。
大概是这样的:
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
-variable中。
您可能需要将字符串分成单词列表。
如果仅使用contains
或indexOf
,则可能给出错误的答案。 例如...
String search = "Doctor Smith went gardening and then went to the cinema on Tuesday";
List<String> words = Arrays.asList("then", "to", "went");
如果使用indexOf
这将给出错误的答案,因为字符序列“ to”出现在单词“ Doctor”中。
这会匹配整个单词(区分大小写)...
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.