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

前端 未结 5 575
轮回少年
轮回少年 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:22

    Not having done any profiling or benchmarking, I'd say it's a fairly safe bet that if you don't need regex magic, then the overhead of the regular expression parser (which you'll get no matter what, in terms of memory as well as CPU usage) costs you a lot more than you can possibly gain on the other end.

提交回复
热议问题