Java String Replacefirst
# 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
* **regex** -- the regular expression to which this string is to be matched.
* **replacement** -- the string to be substituted for the first match.
### Return Value
Returns the resulting String if a match is found, otherwise returns the original String.
### Example
public class Test {public static void main(String args[]) {String Str = new String("hello tutorialοΌI am from tutorialγ");System.out.print("Return Value :" );System.out.println(Str.replaceFirst("tutorial", "google" ));System.out.print("Return Value :" );System.out.println(Str.replaceFirst("(.*)tutorial(.*)", "google" ));}}
The result of executing the above program is:
Return Value :hello googleοΌI am from tutorialγReturn Value :google
* * Java String Class](#)
YouTip