Java String replaceFirst Method

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

Java String ReplaceFirst Syntax

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

Java String.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); // It will return String 

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

Java String ReplaceFirst Example

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

// Java String.replaceFirst Example 
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 public String replaceFirst(String Regexp, String Replacement) 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 Str variable, String.replaceFirst function is replacing the first occurrence

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

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

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

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

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

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

String ReplaceFirst Example 2

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

// Java String.replaceFirst Example 
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 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);
		System.out.println("After Replacing Last Sub String in str1 = " + str5);
		System.out.println("After Replacing String str = " + str6);
		System.out.println("After Replacing String str = " + str7);
		System.out.println("After Replacing String strd = " + str8);
		System.out.println("After Replacing String strd = " + str9);
	}
}
After Replacing First Sub String in str = We are xyz working at abc
After Replacing Last Sub String in str = We are abc working at xyz
After Replacing First Sub String in str1 = Orange Cherry Apple
After Replacing Last Sub String in str1 = Apple Cherry Orange
After Replacing String str = We are xyz
After Replacing String str = TutorialGateway.org
After Replacing String strd = Emp_ID 10 is Working in Sales Depat, Emp20s Emp30s Emp40s
After Replacing String strd = Emp10s Emp20s Emp30s ,Emp_ID 40 is Working in Sales Dept

The following statement calls public String replaceFirst(String Regexp, String Replacement) method to find the first occurred substring ‘abc’ and replace it with substring ‘xyz’.

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

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

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

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

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

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

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

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

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