猿问

如何从句子中的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");

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");

  }

我尝试编写一些代码,但无法弄清楚如何获取 String 的索引"rahul"。


输入:

句子:hii rahul ,nice to meet you .How are you?

搜索词的 ArrayList:["meet","are","rahul"]


预期输出: 索引为 4(rahul句子中的第一个)


Smart猫小萌
浏览 121回答 3
3回答

慕码人8056858

您可以使用String.indexOf(String)来确定子字符串的起始位置:Integer lowestIndex = null;for(String searchWord : search) {&nbsp;&nbsp;&nbsp; &nbsp; int index = sentence.indexOf(searchWord);&nbsp; &nbsp; // update the result if the searchWord occurs at a lower position&nbsp; &nbsp; if (index >= 0 && (lowestIndex == null || lowestIndex > index)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lowestIndex = index;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;}if (lowestIndex == null) {&nbsp; &nbsp; System.out.println("None of the keywords were found");}else {&nbsp; &nbsp; System.out.printf("First keyword at %s%n", lowestIndex);}

largeQ

Matcher m = Pattern.compile("(meet|are|rahul)").matcher(searchText);if (m.find()) {&nbsp; &nbsp; System.out.printf("Found '%s' at position %d%n",&nbsp; &nbsp; &nbsp; &nbsp; m.group(), m.start());}如果你想从一个列表开始:List<String> keywords = Arrays.asList("meet","are","rahul");String pattern = keywords.stream().collect(Collectors.joining("|", "(", ")"));正则表达式搜索速度较慢,但可以添加单词边界\\b(meet|are|rahul),因此找不到“软件”。或者进行不区分大小写的搜索。

MYYA

您可能需要将字符串拆分为单词列表。如果你只使用containsor indexOf,它可能会给出错误的答案。例如...&nbsp; &nbsp; &nbsp; &nbsp; String search = "Doctor Smith went gardening and then went to the cinema on Tuesday";&nbsp; &nbsp; &nbsp; &nbsp; 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 {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String search = "Doctor Smith went gardening then went to the cinema on Tuesday";&nbsp; &nbsp; &nbsp; &nbsp; List<String> words = Arrays.asList("then", "to", "went");&nbsp; &nbsp; &nbsp; &nbsp; int index = 0;&nbsp; &nbsp; &nbsp; &nbsp; int result = -1;&nbsp; &nbsp; &nbsp; &nbsp; String match = null;&nbsp; &nbsp; &nbsp; &nbsp; StringTokenizer tokenizer = new StringTokenizer(search, " ", true);&nbsp; &nbsp; &nbsp; &nbsp; while(result < 0 && tokenizer.hasMoreElements()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String next = tokenizer.nextToken();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(words.contains(next)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = index;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match = next;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index += next.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(match == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Not found.");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Found '" + match + "' at index: " + result);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答