Java String replaceAll Method

The Java String replaceAll method is one of the Java String Method, 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 example. The syntax of the String.replaceAll is as shown below.

Java String ReplaceAll Syntax

The Java Programming Language provides replaceAll method to replace the matching substrings with user-specified String. The Java 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 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 Java 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 Jxvx Progrxmming
We xre xbc working xt xbc compxny
Tutxyrials xyn Java Prxygramming
We are xyz working at xyz company
We are abc working at abc company

Within this replaceall 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 substring ‘xyz’.

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

In the next line, we tried to replace the non-existing substring ‘abx’ with substring ‘xyz’ . From the above screenshot, you can observe 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.

String ReplaceAll Example 2

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

// Java String.replaceAll Example 
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 space.

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

The above statement is returning the output with extra spaces. So, we just altered the above statement to get rid of extra spaces.

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

The following replaceAll 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.