⬅ Previous Topic
Java StringreplaceAll()
methodNext Topic ⮕
Java Stringsplit()
methodreplaceFirst()
method⬅ Previous Topic
Java StringreplaceAll()
methodNext Topic ⮕
Java Stringsplit()
methodThe 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.
public String replaceFirst(String regex, String replacement)
Parameter | Description |
---|---|
regex |
The regular expression pattern to search for. |
replacement |
The string that will replace the first match of the regex. |
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.
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.
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.
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'.
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.
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).
⬅ Previous Topic
Java StringreplaceAll()
methodNext Topic ⮕
Java Stringsplit()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.