Java replaceAll() method

    replaceAll() method

    This method will allow you to replace all the occurrence of the specified string with the provided matching regex and the replacement string or substring.

    Method implementation-

    public String replaceAll(String regex, String replacement) {  
    
            return Pattern.compile(regex).matcher(this).replaceAll(replacement);  
    
    }  

    Syntax-

    public String replaceAll(String regex, String replacement)  

    Example-

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

    Output-

    Example-

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

    Output-

    Java Replace all method