YouTip LogoYouTip

Java String Replacefirst

# Java replaceFirst() Method [![Image 3: Java String Class](#)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](#)
← Java String StartswithJava String Replace β†’