java tutorial - Java replaceAll() method - java programming - learn java - java basics - java for beginners
Description
Java replaceAll() method — replaces each substring of the given string that corresponds to the specified regular expression, with this substitution, in other words, the method allows you to replace the word in a string.
Syntax
Syntax method:
public String replaceAll(String regex, String replacement)
click below button to copy the code. By - java tutorial - team
Options
Detailed information about the parameters:
- regex is a regular expression to which this string must match;
- replacement is a string that will replace the expression found.
Return value
- In Java, replaceAll () returns the result string.
Sample Code
import java.io.*;
public class Test {
public static void main (String args[]) {
String Str = new String ("Welcome to wikitechy.com");
System.out.print ("Return value:");
System.out.println (Str.replaceAll ("(.*) wikitechy (.*)", "IAMGROOT"));
System.out.print ("Return value:");
System.out.println (Str.replaceAll ("wikitechy.com","site dedicated to programming!"));
}
}
click below button to copy the code. By - java tutorial - team
Output
Return value:Welcome to wikitechy.com
Return value:Welcome to site dedicated to programming!