title: Replace words in a string without using replace in Java
body: <p>I am trying to replace all occurrences of a word in a string. However with this code I can only find the first occurrence of it and replace it. Is there any way to expand this code to replace the words in the entire string? I am attempting to do this without using the replace built in methods in Java since I already know how to use those function, I was wondering if there was another way to go about it.  </p>
  
  <pre><code>public static String replace(String old, String newWord, String input) {    
  
  int i = input.indexOf(old);
  
  if (i &lt; 0) {
      return input;
  }
  
      String partBefore = input.substring(0, i);
      String partAfter  = input.substring(i + old.length());
  
      return partBefore + newWord + partAfter;
  }
  </code></pre>