title: String and substring and how many times substring present in main string
body: <p>I have written code by help of some solutions in Google. Can you please help me on what the while loop does in detail?</p>
  
  <pre><code>import java.util.Scanner;
  
  public class TwoStringsWordRepeat {
      public static void main(String[] args) {
          Scanner s = new Scanner (System.in);
          System.out.print("Enter Sentence: ");
          String sentence = s.nextLine();
          System.out.print("Enter word: ");
          String word = s.nextLine();
          int lastIndex = 0;
          int count = 0;
          while (lastIndex != -1) {
              lastIndex = sentence.indexOf(word, lastIndex);
              if (lastIndex != -1) {
                  count++;
                  lastIndex += word.length();
              }
          }
          System.out.println(count);
      }
  }
  </code></pre>
  
  <p>Explain me the code in while loop <code>.indexOf();</code>.</p>