replaceFirst() 方法使用给定的参数 replacement 替换字符串第一个匹配给定的正则表达式的子字符串。
public String replaceFirst(String regex, String replacement)
regex -- 匹配此字符串的正则表达式。
replacement -- 用来替换第一个匹配项的字符串。
成功则返回替换的字符串,失败则返回原始字符串。
public class Test {
public static void main(String args[]) {
String Str = new String("hello itzixishi,I am from itzixishi。");
System.out.print("返回值 :" );
System.out.println(Str.replaceFirst("itzixishi", "google" ));
System.out.print("返回值 :" );
System.out.println(Str.replaceFirst("(.*)itzixishi(.*)", "google" ));
}
}
以上程序执行结果为:
返回值 :hello google,I am from itzixishi。
返回值 :google
©2020 IT自习室京ICP备20010815号