How to parse for words in a string for a specific word in java?



In this article, we will learn how to parse for words in a string for a specific word in Java, i.e., we need to check if a specific word exists in a string, and if it does, we will parse the string to find that word.

There are various methods in Java that you can use to parse a string for a specific word. Here we are going to discuss 3 of them.

Using the contains() method

The contains() method of the String class accepts a sequence of characters and verifies whether it exists in the current String. If found, it returns true; else, it returns false. Following is the syntax of this method -

contains(CharSequence sequence)

Example

We just need to pass the word we want to find in the string to the contains() method, and we will get the result.

public class ParseForWords {
   public static void main(String[] args){
      String str = "Hello how are you, welcome to Tutorialspoint.";
      String wordToFind = "Tutorialspoint";

      if (str.contains(wordToFind)) {
         System.out.println("The word '" + wordToFind + "' is found in the string.");
      } else {
         System.out.println("The word '" + wordToFind + "' is not found in the string.");
      }
   }
}

Output

Following is the output of the above code:

The word 'Tutorialspoint' is found in the string.

Using the indexOf() method

The indexOf() method of the String class accepts a string value and finds the starting index of it in the current String and returns it.

This method returns -1 if it doesn't find the given string in the current one. Following is the syntax -

indexOf(String str)

Example

In the following example, we are trying to search for a particular word in a given text using the indexOf() method -

Just pass the word we want to find in the string to the indexOf() method, and we will get the index of it in the string and get the result.

public class ParseForWords {
   public static void main(String[] args){
      String str = "Hello how are you, welcome to Tutorialspoint.";
      String wordToFind = "Tutorialspoint";

      int index = str.indexOf(wordToFind);
      if (index != -1) {
         System.out.println("The word '" + wordToFind + "' is found at index: " + index);
      } else {
         System.out.println("The word '" + wordToFind + "' is not found in the string.");
      }
   }
}

Output

Following is the output of the above code:

The word 'Tutorialspoint' is found at index: 30

Using the StringTokenizer class

Using the StringTokenizer class, you can divide a String into smaller tokens based on a delimiter and traverse through them.

We need to pass the required text and the delimiter at which the text should be tokenized as parameters to the constructor of this class. The result object holds the tokens, and we can traverse through these tokens using the following methods -

  • hasMoreElements(): Returns true if the current object has more elements.
  • nextToken(): Returns the next token in the current object.

To search/parse for a word using this class, we need to tokenize the given text 

Example

Following example tokenizes all the words in the source string and compares each word init with the given word using the equals() method.

import java.util.StringTokenizer;
public class ParseForWords {
   public static void main(String[] args){
      String str = "Hello how are you, welcome to Tutorialspoint.";
      String wordToFind = "Tutorialspoint";
      boolean found = false;

      StringTokenizer tokenizer = new StringTokenizer(str, " ,.");
      while (tokenizer.hasMoreTokens()) {
         String token = tokenizer.nextToken();
         if (token.equals(wordToFind)) {
            found = true;
            break;
         }
      }

      if (found) {
         System.out.println("The word '" + wordToFind + "' is found in the string.");
      } else {
         System.out.println("The word '" + wordToFind + "' is not found in the string.");
      }
   }
}

Output

Following is the output of the above code:

The word 'Tutorialspoint' is found in the string.
Updated on: 2025-06-05T15:21:38+05:30

912 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements