String (@NotNull CharSequence documentText, @NotNull String newName) { TextRange prevOccurrence = null; StringBuilder result = new StringBuilder(documentText.length() + (newName.length() - myOldName.length()) * myOccurrences.size()); for (TextRange occurrence : myOccurrences) { result.append(documentText.subSequence(prevOccurrence != null ? prevOccurrence.getEndOffset() : 0, occurrence.getStartOffset())); result.append(newName); prevOccurrence = occurrence; } result.append(documentText.subSequence(prevOccurrence != null ? prevOccurrence.getEndOffset() : 0, documentText.length())); return result.toString(); }