Java replace() method

    replace() method

    This method will allow you to provide the new replaced string from the old string.

    Syntax-

    public String replace(char oldChar, char newChar)  

    and

    public String replace(CharSequence target, CharSequence replacement)  

    Example-

    public class Simple{  
       public static void main(String[] args) {      
    
         String s1="welcome to Java";  
         String s2=s1.replace('a','h');
         System.out.println(s2);     
       }
    }  

    Output-

    Java Replace Method

    Example-

    public class Simple{  
       public static void main(String[] args) {      
    
         String s1="welcome to Java";  
         String s2=s1.replace("to","here");
        System.out.println(s2);     
       }
    }  

    Output-

    Java Replace method example