Easy Tutorial
❮ Java9 Completablefuture Api Improvements Java Iterator ❯

Java replaceFirst() Method

Java String Class


The replaceFirst() method replaces the first substring of this string that matches the given regular expression with the given replacement.

Syntax

public String replaceFirst(String regex,
                           String replacement)

Parameters

Return Value

Returns the replaced string if successful, otherwise returns the original string.

Example

public class Test {
    public static void main(String args[]) {
        String Str = new String("hello tutorialpro, I am from tutorialpro.");

        System.out.print("Return Value :" );
        System.out.println(Str.replaceFirst("tutorialpro", "google" ));
        System.out.print("Return Value :" );
        System.out.println(Str.replaceFirst("(.*)tutorialpro(.*)", "google" ));
    }
}

The output of the above program is:

Return Value :hello google, I am from tutorialpro.
Return Value :google

Java String Class

❮ Java9 Completablefuture Api Improvements Java Iterator ❯