FormatData (@NotNull String before) { final String SELECTION_START = "<selection>"; final String SELECTION_END = "<selection/>"; int startOffset = before.indexOf(SELECTION_START); if (startOffset > 0) { int endOffset = before.indexOf(SELECTION_END) - SELECTION_START.length(); String text = before .replace(SELECTION_START, "") .replace(SELECTION_END, ""); return new FormatData(text, startOffset, endOffset); } return new FormatData(before, 0, before.length()); }