Java String replaceFirst Method

The Java String replaceFirst method is one of the String Methods, which is to replace the first occurred substring with the newly specified string.

In this article, we show how to write the String.replaceFirst method in Java Programming Language with an example, and the basic syntax of it is as shown below.

Java String ReplaceFirst Syntax

This Java Programming Language provides replaceFirst Method to replace the first substring with a user-specified String.

The Java replaceFirst 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 replaceFirst(String Regexp, String Replacement); 

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

Java String ReplaceFirst Example

The replaceFirst replaces the first occurred substring with a user-specified new string in an object. In this Java Program, we use the substring as the first argument. It may help to understand the basic functionality of the String function.

package StringFunctions;

public class ReplaceFirst {
	public static void main(String[] args) {
		String str = "Tutorials on Java Programming";
		String str1 = "We are abc working at abc company";
		
		String str2 = str.replaceFirst("a", "NEW");
		String str3 = str1.replaceFirst("a", "X");
		String str4 = str.replaceFirst("Java", "Java Script");
		String str5 = str1.replaceFirst("abc", "xyz");
		String str6 = str1.replaceFirst("abx", "xyz");

		System.out.println("After Replacing First String str = " + str2);
		System.out.println("After Replacing First String str1 = " + str3);
		System.out.println("After Replacing First String str = " + str4);
		System.out.println("After Replacing First String str1 = " + str5);
		System.out.println("After Replacing First String str1 = " + str6);
	}
}
Java String replaceFirst Method 1

The following statement will call the Java String replaceFirst method to find the substring ‘a’ and replace it with ‘x’. If you observe the above screenshot, Although ‘a’ substring is repeated four times in the Str variable, this function is replacing the first occurrence.

str2 = str.replaceFirst("a", "NEW");

The following Java String replaceFirst statement will find the substring ‘abc’ and replace it with ‘xyz’. From the above screenshot, Although ‘abc’ substring is repeated twice in the Str variable, it is replacing the first occurrence.

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

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

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

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

replaceFirst Example 2

This program helps to understand the use of regular expressions in replaceFirst method practically.

package StringFunctions;

public class ReplaceFirst2 {
	public static void main(String[] args) {
		String str = "We are abc working at abc";
		String str1 = "Apple Cherry Apple";
		String strd = "Emp10s Emp20s Emp30s Emp40s";
		
		String str2 = str.replaceFirst("abc", "xyz");
		String str3 = str.replaceFirst("abc$", "xyz");
		String str4 = str1.replaceFirst("Apple", "Orange");
		String str5 = str1.replaceFirst("Apple$", "Orange");
		String str6 = str.replaceFirst("abc(.*)", "xyz");
		String str7 = str.replaceFirst("(.*)abc(.*)", "TutorialGateway.org");
		String str8 = strd.replaceFirst("Emp\\d\\ds", "Emp_ID 10 is Working in Sales Depat,");
		String str9 = strd.replaceFirst("Emp\\d\\ds$", ",Emp_ID 40 is Working in Sales Dept");

		System.out.println("After Replacing First in str = " + str2);
		System.out.println("After Replacing Last in str = " + str3);
		System.out.println("After Replacing First in str1 = " + str4);
		System.out.println("After Replacing Last in str1 = " + str5);
		System.out.println("str = " + str6);
		System.out.println("str = " + str7);
		System.out.println("strd = " + str8);
		System.out.println("strd = " + str9);
	}
}
After Replacing First in str = We are xyz working at abc
After Replacing Last in str = We are abc working at xyz
After Replacing First in str1 = Orange Cherry Apple
After Replacing Last in str1 = Apple Cherry Orange
str = We are xyz
str = TutorialGateway.org
strd = Emp_ID 10 is Working in Sales Depat, Emp20s Emp30s Emp40s
strd = Emp10s Emp20s Emp30s ,Emp_ID 40 is Working in Sales Dept

The following statement calls replaceFirst method to find the first occurred substring ‘abc’ and replace it with ‘xyz’.

str2 = str.replaceFirst("abc", "xyz");

The following Java String replaceFirst statement will find the last occurred substring ‘abc’ and replace it with ‘xyz’. See from the above, Although ‘abc’ substring is repeated twice in the Str variable, it is replacing the last occurred.

str3 = str.replaceFirst("abc$", "xyz");

It will find the substring ‘abc’ and replace the ‘abc’, the string after the ‘abc’ with ‘xyz’.

str6 = str.replaceFirst("abc(.*)", "xyz");

This replaceFirst statement will find the substring ‘abc’ and replace the ‘abc’, the string before it, string after the ‘abc’ with ‘xyz’.

str7 = str.replaceFirst("(.*)abc(.*)", "TutorialGateway.org");

The following statement will find the first occurred substring ‘Emp\\d\\ds’ and replace it with ‘Emp_ID 10 is Working in Sales Depat,”. Here, \\d\\ means integer value in between the Emp and ds.

str8 = strd.replaceFirst("Emp\\d\\ds", "Emp_ID 10 is Working in Sales Depat,");