This String class method, replaceAll(), replaces all the occurrences of a word in a string with another word introduced with JDK 1.4 to use with regular expressions. The method returns a new string with affected words. The original string is not disturbed (string is immutable).
We discussed a similar program in "String – uppercase, lowercase, replacing" where replace() method usage is shown.
Following are the two methods involved along with String replaceAll().
- String replaceAll(String regexp, String target): Replaces all the words or a substring (group of words) that matches the regular expression with the target string.
- String replaceFirst(String regexp, String target): Replaces the first word or a substring (group of words) that matches the regular expression with the target string.
Following program shows the simplest way of using the String replaceAll() method.
public class ReplaceAll
{
public static void main(String args[])
{ // REPLACING ALL OCCURRENCES
String str1 = "Hard work results. Hard work persists.";
System.out.println("Before replaceAll(): " + str1);
String str2 = str1.replaceAll("Hard work", "Dedication");
System.out.println("After replaceAll(): " + str2);
// REPLACING FIRST OCCURRENCE ONLY
String str3 = str1.replaceFirst("Hard work", "Dedication");
System.out.println("After replaceFirst(): " + str3);
}
}

Output screen on String replaceAll() Java
String str2 = str1.replaceAll(“Hard work”, “Dedication”);
The above statement can be replaced with replace() method as follows and works nice.
String str2 = str1.replace (“Hard work”, “Dedication”);
A special case of repalceAll() is replaceFirst() which replaces the first occurrence only.
String str3 = str1.replaceFirst(“Hard work”, “Dedication”);
In the above statement, the first occurrence of "Hard work" is replaced by "Dedication". Observe the screenshot.
But replaceAll() method is meant to work with regular expressions, a concept supported by Java from JDK 1.4. Observe the method signatures.
Following program uses regular expressions to replace the words of a string.
public class ReplaceAll
{
public static void main(String args[])
{
String str1 = "a 1 bc 2 def 3 gh456";
String str2 = str1.replaceAll("\\d", "OK");
System.out.println(str2);
String str3 = str1.replaceAll("\\D", "OK");
System.out.println(str3);
String str4 = "Hello Hello Hello";
System.out.println(str4.replaceFirst("Hello", "Morning"));
String str5 = str4.replaceAll("^Hello", "Morning");
System.out.println(str5);
System.out.println(str4.replaceAll("Hello$", "Morning"));
}
}

Output screen on String replaceAll() Java
String str1 = “a 1 bc 2 def 3 gh456”;
String str2 = str1.replaceAll(“\\d”, “OK”);
In the string str1, both digits and letters (alphabets) exist. The regular expression syntax \\d replaces each digit with OK.
String str3 = str1.replaceAll(“\\D”, “OK”);
The regular expression syntax \\D replaces each non-digit (each letter), in the string str1, with OK.
String str4 = “Hello Hello Hello”;
System.out.println(str4.replaceFirst(“Hello”, “Morning”));
We know in the earlier program, the replaceFirst() method replaces the first occurrence of Hello with Morning.
String str5 = str4.replaceAll(“^Hello”, “Morning”);
The same affect of repalceFirst() can be achieved with regular expression. The expression ^Hello indicates the first occurrence of Hello to be replaced with Morning.
System.out.println(str4.replaceAll(“Hello$”, “Morning”));
No method exists with String class to change the last the occurrence of Hello. This can be achieved with the regular expression. The expression Hello$ indicates the last occurrence of Hello to be replaced with Morning.
Following are some regular expressions suitable to use with replaceAll().
Regular Expression | Description |
---|---|
\w | A word character |
\W | A non-word character |
\d | Any digit |
\D | Any non-digit (it includes whitespace also) |
\s | Whitespace |
\S | A non-whitespace character |
Regular expressions are discussed, to some extent, in way2java.com in JDK 1.4 (J2SE 4) Version. But regular expressions usage is a big subject which requires a good study. You are advised to refer a good Web site that primarily discusses regular expressions.
Java split() method is illustrated in Region Matches – Interning – Splitting and JDK 1.4 (J2SE 4) Version.
sir it is very clear… really awesome sir…sir suggest me a website for sql.