String replaceAll() Java

String replaceAll() Java


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().
  1. 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.
  2. 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.



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.



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.

Leave a Reply

Your email address will not be published. Required fields are marked *