Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does string.replaceAll() performance suffer from string immutability?

Lets say I called replaceAll() on a big string that replaced 1,000 matching instances. Does it mean that 1,000 strings were created and reassigned in process because of string immutability? Is there any faster alternatives?

like image 276
serg Avatar asked Aug 09 '09 00:08

serg


1 Answers

If you dig into String, you'll see that it delegates replaceAll() to Pattern & Matcher and Matcher.replaceAll() uses a StringBuilder to store the eventually returned value.

So no, String.replaceAll() does not create more than a small number of objects.

like image 160
Kevin Montrose Avatar answered Sep 20 '22 00:09

Kevin Montrose