ReplaceInfo (final String oldText, final String newText) { if (oldText.equals(newText)) { return new ReplaceInfo(-1, -1, null); } final int oldLength = oldText.length(); final int newLength = newText.length(); int startOffset = 0; while ( startOffset < oldLength && startOffset < newLength && oldText.charAt(startOffset) == newText.charAt(startOffset) ) { startOffset++; } int endOffset = oldLength; while (true) { if (endOffset <= startOffset) { break; } final int idxInNew = newLength - (oldLength - endOffset) - 1; if (idxInNew < startOffset) { break; } final char c1 = oldText.charAt(endOffset - 1); final char c2 = newText.charAt(idxInNew); if (c1 != c2) { break; } endOffset--; } return new ReplaceInfo(startOffset, endOffset, newText.substring(startOffset, newLength - (oldLength - endOffset))); }