java.util.regex.Matcher.replaceFirst(String replacement)
方法是用来进行字符串的替换操作。
public String replaceFirst(String replacement) 替换匹配到的第一个符合的子串。
eg:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo {
private static String a= "am";
private static String b= "The am ss am te.";
private static String c = "mm";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(a);
Matcher matcher = pattern.matcher(b);
b= matcher.replaceFirst(c);
System.out.println(b);
}
}
结果为:
The mm ss am te.