Java String replaceAll Method

The Java replaceAll method is one of the String methods, which is to replace each substring that matches the regular expression with a newly specified string.

In this article, we will show how to write the String.replaceAll method in Java Programming Language with an example. The syntax of the String.replaceAll is as shown below.

Java String ReplaceAll Syntax

The Java Programming Language provides the replaceAll method to replace the matching substrings with a user-specified String. The replaceAll method accepts two string arguments. The first argument (regex) is the regular expression that represents a substring existing in the String_Object, and the second argument (Replacement) is the substring that you want to replace with.

public String replaceAll(String Regexp, String Replacement);

//In order to use in program
String_Object.replaceAll(Regexp, Replacement);
  • String_Object: Please specify the valid object on which we want to perform Replacement.
  • Regexp: Please specify the valid one that we want to change or the regular expression that represents the String. Whatever you place here, this method will replace it with a new string (Replacement).
  • Replacement: Please specify the valid new one you want to insert into String_Object.

Java String ReplaceAll Example

The replaceAll replaces the matched substring with a user-specified new string object.

In this Java program, we will use the substring as the first argument. It may help to understand the basic functionality of the replaceAll method.

package StringFunctions;

public class ReplaceAllMethod {
	public static void main(String[] args) {
		String str = "Tutorials on Tava Programming";
		String str1 = "We are abc working at abc company";
		String str2 = str.replaceAll("a", "x");
		String str3 = str1.replaceAll("a", "x");
		String str4 = str.replaceAll("o", "xy");
		String str5 = str1.replaceAll("abc", "xyz");
		String str6 = str1.replaceAll("abx", "xyz");

Tutorixls on Txvx Progrxmming
We xre xbc working xt xbc compxny
Tutxyrials xyn Tava Prxygramming
We are xyz working at xyz company
We are abc working at abc company

Within this example, the following statement will call the replaceAll method to find the substring ‘a’ and replace it with ‘x’.

str2 = str.replaceAll("a", "x");

The following Java statement will find the substring ‘abc’ and replace it with the substring ‘xyz’.

str5 = str1.replaceAll("abc", "xyz");

In the next line, we tried to replace the non-existing substring ‘abx’ with the substring ‘xyz’. The above screenshot shows that it is not replacing anything (String_Object is unchanged).

str6 = str1.replaceAll("abx", "xyz");

Lastly, we used the System.out.println statements to print the output.

Example 2

This program will help to understand the use of regular expressions in the replaceAll method practically.

package StringFunctions;

public class ReplaceAllMethod2 {
	public static void main(String[] args) {
		String str = "We are abc working at Abc";
		String strd = "Hi 10, How 20 and 30 are doing!";
		String str2 = strd.replaceAll("[0-9]+", "");
		String str3 = strd.replaceAll(" [0-9]+", "");
		String str4 = str.replaceAll("[a-zA-Z]+", "Java");

		System.out.println("After Replacing First Sub String in str = " + str2);
		System.out.println("After Replacing Last Sub String in str = " + str3);
		System.out.println("After Replacing First Sub String in str1 = " + str4);
Java String replaceAll Method 2

The following statement will call the replaceAll method to find the substring ‘[0-9]’ and replace it with the “”. It means it will replace the numbers between 0 and 9 with empty spaces.

str2 = strd.replaceAll("[0-9]+", "");

The above statement returns the output with extra spaces. So, we just altered the above statement to eliminate extra spaces.

str3 = strd.replaceAll(" [0-9]+", "");

The following String function statement will find the substring ‘[a-zA-Z]’ and replace it with “Java”. It means it will replace the alphabets between a and z, A, and Z with Java.

str4 = str.replaceAll("[a-zA-Z]+", "Java");

Comments are closed.