String (@NotNull String text, @NotNull List<String> from, @NotNull List<String> to) { assert from.size() == to.size(); StringBuilder result = null; replace: for (int i = 0; i < text.length(); i++) { for (int j = 0; j < from.size(); j += 1) { String toReplace = from.get(j); String replaceWith = to.get(j); final int len = toReplace.length(); if (len == 0) continue; if (text.regionMatches(i, toReplace, 0, len)) { if (result == null) { result = new StringBuilder(text.length()); result.append(text, 0, i); } result.append(replaceWith); //noinspection AssignmentToForLoopParameter i += len - 1; continue replace; } } if (result != null) { result.append(text.charAt(i)); } } return result == null ? text : result.toString(); }