[英]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.