Today, suddenly we saw in Java replace There are two ways, one is a direct replacement, the other can match alternative ways:
public String replace(CharSequence target, CharSequence replacement)
-
Alternatively replacement sequence substring of this string literal match all of the target sequence using the specified literal. The replacement is performed from the beginning toward the end of the string, for example, replaced by "b" string "aaa" in the "aa" generates "ba" instead of "ab".
-
-
- parameter:
-
target
- to be replaced char value sequence -
replacement
- replacement sequence char value - return:
- The resulting String
- Throws:
-
NullPointerException
- If youtarget
orreplacement
isnull
.
Source as follows:
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}