Java Reference
In-Depth Information
The loop continues as long as there are matches to sun . On each itera-
tion through the loop, all the characters from the append position (the
position after the last match; initially zero) to the start of the current
match are copied into the string buffer. Then the replacement string moon
is copied. When there are no more matches, appendTail copies any re-
maining characters into the buffer.
The replacement methods of Matcher are
public String replaceFirst(String replacement)
Replaces the first occurrence of this matcher's pattern with
the replacement string, returning the result. The matcher is
first reset and is not reset after the operation.
public String replaceAll(String replacement)
Replaces all occurrences of this matcher's pattern with the re-
placement string, returning the result. The matcher is first re-
set and is not reset after the operation.
public Matcher appendReplacement(StringBuffer buf, String replace-
ment)
Adds to the string buffer the characters between the current
append and match positions, followed by the replacement
string, and then moves the append position to be after the
match. As shown above, this can be used as part of a replace-
ment loop. Returns this matcher.
public StringBuffer appendTail(StringBuffer buf)
Adds to the string buffer all characters from the current ap-
pend position until the end of the sequence. Returns the buf-
fer.
So the previous example can be written more simply with replaceAll :
 
Search WWH ::




Custom Search