Replace all occurrences of substring in a string - which is more efficient in Java?

前端 未结 5 569
轮回少年
轮回少年 2020-12-15 09:53

I know of two ways of replacing all occurrences of substring in a string.

The regex way (assuming \"substring-to-be-replaced\" doesn\'t include regex specia

5条回答
  •  独厮守ぢ
    2020-12-15 10:28

    Shouldn't you compare replaceAll 2 times? However, for a single invocation it will hardly be measurable. And will you do millions of comparisions?

    Then I would expect 'compile' to be faster, but only, if you don't use a constant String without any pattern-rules.

    Where is the problem in writing a micro benchmark? Or look up the source.

提交回复
热议问题