Java String replaceFirst() Method

The String.replaceFirst() method in Java is used to replace the first substring of a string that matches a given regular expression with a specified replacement string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. replaceFirst Method Syntax
  3. Examples
    • Replacing the First Occurrence Using a Regular Expression
    • Replacing the First Digit with a String
    • Handling Special Characters
  4. Conclusion

Introduction

The String.replaceFirst() method is a member of the String class in Java. It allows you to replace the first substring of a string that matches a given regular expression with a specified replacement string. This method is particularly useful when you need to perform a pattern-based replacement for the first occurrence only.

replaceFirst Method Syntax

The syntax for the replaceFirst method is as follows:

public String replaceFirst(String regex, String replacement)
  • regex: The regular expression to match the substring to be replaced.
  • replacement: The string to replace the matched substring.

Examples

Replacing the First Occurrence Using a Regular Expression

The replaceFirst method can be used to replace the first occurrence of a pattern in a string with a specified replacement string.

Example

public class ReplaceFirstExample {
    public static void main(String[] args) {
        String message = "The rain in Spain stays mainly in the plain.";

        String newMessage = message.replaceFirst("ain", "ane");

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: The rain in Spain stays mainly in the plain.
New message: The rane in Spain stays mainly in the plain.

Replacing the First Digit with a String

The replaceFirst method can be used to replace the first digit in a string with a specified replacement string.

Example

public class ReplaceFirstExample {
    public static void main(String[] args) {
        String message = "Order number: 12345";

        String newMessage = message.replaceFirst("\\d", "#");

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: Order number: 12345
New message: Order number: #2345

Handling Special Characters

The replaceFirst method can handle special characters in regular expressions by using escape sequences.

Example

public class ReplaceFirstExample {
    public static void main(String[] args) {
        String message = "Price: $10.00";

        String newMessage = message.replaceFirst("\\$", "USD ");

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: Price: $10.00
New message: Price: USD 10.00

Removing Leading Whitespace

The replaceFirst method can be used to remove leading whitespace from a string.

Example

public class ReplaceFirstExample {
    public static void main(String[] args) {
        String message = "   Java is fun";

        String newMessage = message.replaceFirst("^\\s+", "");

        System.out.println("Original message: '" + message + "'");
        System.out.println("New message: '" + newMessage + "'");
    }
}

Output:

Original message: '   Java is fun'
New message: 'Java is fun'

Conclusion

The String.replaceFirst() method in Java is sued for replacing the first substring based on regular expressions. By understanding how to use this method, you can efficiently perform pattern-based replacements in your Java applications. Whether you are replacing the first occurrence of a specific substring, digit, special character, or leading whitespace, the replaceFirst method provides a reliable solution for these tasks.

Comments