String replaceFirst() method - Syntax and Examples
Learn how to use the Java `replaceFirst()` method. This tutorial provides a clear explanation of its syntax, parameters, return value, and practical examples with code and output.
This reference explains String replaceFirst() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
String replaceFirst() method - Syntax and Examples
The replaceFirst() method in Java is a handy tool for substituting the first occurrence of a regular expression pattern within a string with another specified string. It's similar to replaceAll() but targets only the initial match.
Syntax
public String replaceFirst(String regex, String replacement)
Parameters
| Parameter | Description |
|---|---|
regex |
The regular expression pattern to search for. |
replacement |
The string that will replace the first match of the regex. |
Return Value
The replaceFirst() method returns a new string with the first occurrence of the regular expression replaced by the specified replacement string. If the pattern is not found, it returns the original string.
Examples
Replacing a Single Word
This example demonstrates how to replace the first instance of a specific word in a sentence.
String str = "Java is fun, Java is powerful.";
String newStr = str.replaceFirst("Java", "Python");
System.out.println(newStr);
Python is fun, Java is powerful.
Explanation: The code replaces the first occurrence of 'Java' with 'Python'. Notice that only the first 'Java' is modified.
Replacing a Number
This example shows how to replace the first number found in a string.
String str = "My age is 25, and my friend's age is 30.";
String newStr = str.replaceFirst("[0-9]+", "35");
System.out.println(newStr);
My age is 35, and my friend's age is 30.
Explanation: The regular expression '[0-9]+' matches one or more digits. The first sequence of digits (25) is replaced with 35.
Replacing a Character Sequence
This example demonstrates replacing the first occurrence of a character sequence using `replaceFirst()`
String str = "Hello world, hello Java";
String newStr = str.replaceFirst("hello", "Goodbye");
System.out.println(newStr);
Hello world, Goodbye Java
Explanation: The first occurrence of 'hello' (case-sensitive) is replaced with 'Goodbye'.
No Match Found
This example illustrates what happens when the regular expression pattern isn’t found in the string.
String str = "This is a test.";
String newStr = str.replaceFirst("xyz", "abc");
System.out.println(newStr);
This is a test.
Explanation: Because the string 'xyz' doesn’t exist in the original string, replaceFirst() returns the original string unchanged.
Using Capturing Groups
You can utilize capturing groups within your regular expression and refer to them in the replacement string using $1, $2, etc. This example replaces a date format.
String str = "Date: 2023-10-27";
String newStr = str.replaceFirst("(\d{4})-(\d{2})-(\d{2})", "$2/$3/$1");
System.out.println(newStr);
Date: 10/27/2023
Explanation: The regex captures the year, month, and day into groups. The replacement string rearranges them to create a different date format using $1 (year), $2 (month) and $3 (day).
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.