Replace a substring inside a string by another one in Java

This example describes how replace method of java String class can be used to replace character or substring by new one.
public class StringReplace{
   public static void main(String args[]){
     
      String str = "Hello World";
      System.out.println(str.replace('H''W'));
      System.out.println(str.replaceFirst("He", "Wa"));
      System.out.println(str.replaceAll("He", "Ha"));
   }
}
Result:
The above code sample will produce the following result.
Wello World
Wallo World
Hallo World


Source code: