String (@NotNull String text, @NotNull String oldS, @NotNull String newS, boolean ignoreCase) { if (text.length() < oldS.length()) return text; StringBuilder newText = null; int i = 0; while (i < text.length()) { int index = ignoreCase? indexOfIgnoreCase(text, oldS, i) : text.indexOf(oldS, i); if (index < 0) { if (i == 0) { return text; } newText.append(text, i, text.length()); break; } else { if (newText == null) { if (text.length() == oldS.length()) { return newS; } newText = new StringBuilder(text.length() - i); } newText.append(text, i, index); newText.append(newS); i = index + oldS.length(); } } return newText != null ? newText.toString() : ""; }