Java String.replaceFirst() Method Example

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

Java String.replaceFirst() Method Example

This is a complete example to demonstrate the usage of replaceFirst()  methods.
public class ReplaceExample {
    public static void main(String[] args) {
        String str = "javaguides";
        String subStr = str.replaceFirst("[a-z]", "java");
        System.out.println(subStr);
    }
}

Output:
javaavaguides 

Reference


Comments