The fact that the replace method returns a string object rather than replacing the contents of a given string is a little obtuse (but understandable when you know that strin
If you have a number of strings to replace (such as XML escape sequences), especially where the replacements are different length from the pattern, FSM lexer type algorithm seems like it might be most efficient, similar to the suggestion of processing in a stream fashion, where the output is incrementally built.
Perhaps a Matcher object could be used to do that efficiently.