Faster alternatives to replace method in a Java String?

后端 未结 10 1402
佛祖请我去吃肉
佛祖请我去吃肉 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:55

    When you're replacing single characters, consider iterating over your character array but replace characters by using a (pre-created) HashMap().

    I use this strategy to convert an integer exponent string by unicode superscript characters.

    It's about twice as fast compared to String.replace(char, char). Note that the time associated to creating the hash map isn't included in this comparison.

提交回复
热议问题