by Dinesh Thakur

String replaceFirst (String regex, String replace): This method replaces the first substring in the invoking string that matches the given regular expression with the specified replacement string replace. For example,

s3.replaceFirst("is","was");

It will replace the first occurrence of substring is with the substring was in the given string.

public class StringReplaceFirst

{
     public static void main(String[] args)
     {
            String str3 = "Where there is a Will , there is a way";
            System.out.println("str3.replaceFirst(\"is\" ,\"was\") = " +str3.replaceFirst("is" ,"was"));
      }
}

String replaceFirst() in Java Example