Java String ReplaceAll() Method



This process restores each substring of this thread that competitions the given normal face with the given substitute. The replaceAll() process restores each substring of this string (the String on which it is called) that contests the specified standard expression with the given substitute. It internally uses classes like example and Matcher from java. util. regex package for penetrating and replacing matching values or substring.


Here is the syntax of this method:

public String replaceAll(String regex, String replacement)


Here is the detail of parameters −

  • regex − the regular expression to which this string is to be matched.
  • replacement − − the string which would replace found expression.

Return Value

This method returns the resulting String.


import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to Intellinuts.com");

      System.out.print("Return Value :" );
      System.out.println(Str.replaceAll("(.*)INTELLI(.*)", "SMROOD"));

This will produce the following result:


Return Value :SMROOD

Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.