Java Data Type Tutorial - Java String.replaceFirst(String regex, String replacement)








Syntax

String.replaceFirst(String regex, String replacement) has the following syntax.

public String replaceFirst(String regex,   String replacement)

Example

In the following code shows how to use String.replaceFirst(String regex, String replacement) method.

public class Main {

  public static void main(String args[]) {

    String str = "This is a test. This is a test from java2s.com";

    System.out.println(str.replaceFirst("Th", "Ab"));
  }
}

The code above generates the following result.