Java Replace All Occourances Of String Example
Chapter:
Regular Expression
Last Updated:
13-09-2016 17:17:08 UTC
Program:
/* ............... START ............... */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaReplaceAllOccourances {
public static void main(String args[]) {
Pattern p = Pattern.compile("hello");
String instring = "hello hello hello.";
System.out.println("initial String: " + instring);
Matcher m = p.matcher(instring);
String tmp = m.replaceAll("Java");
System.out.println("String after replacing 1st Match: " + tmp);
}
}
/* ............... END ............... */
Output
initial String: hello hello hello.
String after replacing 1st Match: Java Java Java.
Tags
Replace All Occourances Of String, Java, Regular Expression, replaceAll() method of Matcher class