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?
Lets say I called replaceAll() on a big string that replaced 1,000 matching instances.
Share
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.