The appendReplacement(StringBuffer sb, String replacement) method

This method is intended to be used in a loop together with the appendTail and find methods. Once we get a match using the find() method, we can call the appendReplacement() method to operate on each match and replace the matched text. Finally, it appends the replaced text to StringBuffer. It reads characters from the input sequence, starting at the append position, and appends them to the given string buffer. It stops after reading the last character preceding the previous match, that is, the character at index start() - 1.

The replacement string may contain references to subsequences captured during the previous match. All the rules of replacement reference we String.replaceAll apply to this method also.

The appendReplacement() method keeps track of what has been copied into StringBuffer, so we can keep searching for matches using find() in a loop, until no more matches are found in the input text. There will be an example following the next section.

Java 9 has another overloaded signature of this method to start accepting StringBuilder instead of StringBuffer as the first parameter.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.206.25