今天突然看到Java中的replace有两种方法,一种是直接替换,另一种是可以进行匹配替换的方式:
public String replace(CharSequence target, CharSequence replacement)
target
- 要被替换的 char 值序列replacement
- char 值的替换序列NullPointerException
- 如果 target
或 replacement
为 null
。源码如下:
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
原文:https://www.cnblogs.com/sharysea/p/10965877.html