Latest web development tutorials

Java replaceFirst () method

Java String class Java String class


replaceFirst () method using the given parameters of the first replacement replacement string matches the given regular expression substring.

grammar

public String replaceFirst(String regex,
                           String replacement)

parameter

  • regex - matches the regular expression string.

  • replacement - to replace the first occurrence of the string.

return value

The success of the replacement string is returned, then failed to return the original string.

Examples

public class Test {
	public static void main(String args[]) {
		String Str = new String("hello w3big,I am from w3big。");

		System.out.print("返回值 :" );
		System.out.println(Str.replaceFirst("w3big",
		                         "google" ));
		System.out.print("返回值 :" );
		System.out.println(Str.replaceFirst("(.*)w3big(.*)",
		                         "google" ));
	}
}

The above program execution results:

匹配成功返回值 :w3big
匹配失败返回值 :www.google.com

Java String class Java String class