// no guarantees are made if you supply overlapping/out-of-bounds ranges sS replaceCharRanges(S text, LPair replacements) { if (empty(replacements)) ret text; replacements = sortByPairsA(replacements); int iChar = 0, i = 0; new StringBuilder buf; while (i < l(replacements)) { Pair p = replacements.get(i++); buf.append(substring(text, iChar, p.a.start)); buf.append(p.b); iChar = p.a.end; } if (iChar < l(text)) buf.append(substring(text, iChar)); ret str(buf); }