How can we check if specific string occurs multiple times in another string in Java?

Java 8Object Oriented ProgrammingProgramming

You can find whether a String contains a specified sequence of characters using any of the methods −

  • 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.

  • The contains() method − The contains a () method of the String class accepts a sequence of characters value and verifies whether it exists in the current String. If found it returns true else it returns false.

In addition to these, you can also use the Split() method of the String class. This method accepts a String value representing a delimiter, splits the current String based on the given delimiter and returns a String array containing tokens of the String.

You can split the string into an array of words using this method and compare each word with the required word manually.


Following Java example reads the contents of a file into a String, accepts a word from the user and prints the number of times the given word occurred in the String (file).

 Live Demo

import java.util.Scanner;
public class StringOccurrence {
   public static String fileToString(String filePath){
      Scanner sc = null;
      String input = null;
      StringBuffer sb = null;
      try {
         sc = new Scanner(new File(filePath));
         sb = new StringBuffer();
         while (sc.hasNextLine()) {
            input = sc.nextLine();
            sb.append(" "+input);
      catch(Exception ex) {
      System.out.println("Contents of the file: ");
      return sb.toString();
   public static void main(String args[]) throws FileNotFoundException {
      //Reading the word to be found from the user
      String filePath = "D://sampleData.txt";
      Scanner sc = new Scanner(;
      System.out.println("Enter the word to be found");
      String word =;
      boolean flag = false;
      int count = 0;
      String text = fileToString(filePath);
      String textArray[] = text.split(" ");
      for(int i=0; i<textArray.length; i++) {
         if(textArray[i].equals(word)) {
            flag = true;
            count = count+1;
      if(flag) {
         System.out.println("Number of occurrences is: "+count);
      } else {
         System.out.println("File does not contain the specified word");


Enter the word to be found
Contents of the file:
Tutorials Point originated from the idea that there exists a class of readers who respond better
 to online content and prefer to learn new skills at their own pace from the comforts of their
drawing rooms. The journey commenced with a single tutorial on HTML in 2006 and elated by the 
response it generated, we worked our way to adding fresh tutorials to our repository which now
 proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming 
languages to web designing to academics and much more. 40 million readers read 100 million pages 
every month. Our content and resources are freely available and we prefer to keep it that way 
to encourage our readers acquire as many skills as they would like to. We don’t force our 
readers to sign up with us or submit their details either. No preconditions and no impediments. 
Simply Easy Learning!
Number of occurrences is: 4
Published on 10-Sep-2019 16:54:09