PrajaktaParkhade
PrajaktaParkhade

Reputation: 115

how to get the index for first occurence of any word from arraylist in sentence

I want to get the index of word from the sentence. But here I don't want to check for one specific word. I have list of words and I want to get index of the first occurrence of any word from the list which available in the sentence.
I want the index to get the substring of the sentence, starting at the resulted index.

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

I tried writing some code, but could not figure out how to get the index of the String "rahul".

Input:
Sentence: hii rahul ,nice to meet you .How are you?
ArrayList of searched words: ["meet","are","rahul"]

Expected output: Index is 4 (as the rahul comes first in the sentence)

Upvotes: 0

Views: 1376

Answers (5)

BretC
BretC

Reputation: 4209

You probably need to split your string into a list of words.

If you just use contains or indexOf, it may give the wrong answer. For example...

        String search = "Doctor Smith went gardening and then went to the cinema on Tuesday";
        List<String> words = Arrays.asList("then", "to", "went");

This would give the wrong answer if using indexOf because the character sequence 'to' appears within the word 'Doctor'.

This does a match on whole words (case sensitive)...

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

Upvotes: 1

Adriaan Koster
Adriaan Koster

Reputation: 16209

You can use String.indexOf(String) to determine the starting position of a substring:

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

Upvotes: 4

Joop Eggen
Joop Eggen

Reputation: 109595

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());
}

If you want to start with a List:

List<String> keywords = Arrays.asList("meet","are","rahul");
String pattern = keywords.stream().collect(Collectors.joining("|", "(", ")"));

A regular expression search is slower, but one could add word boundaries \\b(meet|are|rahul) so "software" is not found. Or do a case-insensitive search.

Upvotes: 2

Kevin Cruijssen
Kevin Cruijssen

Reputation: 9326

Something like this perhaps:

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

If the word is not found .indexOf will return -1. If it is found, we save the lowest in the firstIndex-variable.

Try it online.

Upvotes: 1

Stanislav Shamilov
Stanislav Shamilov

Reputation: 1828

You can use String.indexOf method. But be aware that indexing starts from 0, so in your example the output will be 4.

Upvotes: 1

Related Questions