假设我在替换了 1,000 个匹配实例的大字符串上调用了 replaceAll()。这是否意味着由于字符串不可变性而在过程中创建并重新分配了 1,000 个字符串?有没有更快的替代方案?
最佳答案
如果你深入研究 String ,您会看到它将 replaceAll() 委托(delegate)给 Pattern & Matcher 和 Matcher.replaceAll() 使用StringBuilder 来存储最终返回的值。
所以不,String.replaceAll() 不会创建超过少量的对象。
关于java - string.replaceAll() 性能是否受到字符串不变性的影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1250216/