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 String.replaceAll method to replace the matching substrings with user-specified String. The Java String.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); // It will return String 

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

Java String ReplaceAll Example

The String.replaceAll replaces the matched substring with a user-specified new string in a 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 string.replaceAll method.

// Java String.replaceAll Example 
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");

		System.out.println("After Replacing String str = " + str2);
		System.out.println("After Replacing String str1 = " + str3);
		System.out.println("After Replacing String str = " + str4);
		System.out.println("After Replacing String str1 = " + str5);
		System.out.println("After Replacing String str1 = " + str6);
	}
}
After Replacing String str = Tutorixls on Jxvx Progrxmming
After Replacing String str1 = We xre xbc working xt xbc compxny
After Replacing String str = Tutxyrials xyn Java Prxygramming
After Replacing String str1 = We are xyz working at xyz company
After Replacing String str1 = We are abc working at abc company

Within this string replaceall example, the following statement will call the public String replaceAll(String Regexp, String Replacement) method to find the substring ‘a’ and replace it with ‘x’.

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

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

String 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).

String 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 string.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 public String replaceAll(String Regexp, String Replacement) method to find the substring ‘[0-9]’ and replace it with the substring “”. It means it will replace the numbers between 0 and 9 with empty space.

String 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.

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

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

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

Comments are closed.