Faster alternatives to replace method in a Java String?

后端 未结 10 1404
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 11:07

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

10条回答
  •  轮回少年
    2020-12-14 11:43

    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.

提交回复
热议问题