TextRange (String text, int start, int end, char startChar, char endChar) { int prev = text.lastIndexOf(startChar, start); int next = text.indexOf(endChar, end); if (prev < 0 || next < 0) { return null; } if (StringUtil.contains(text, prev + 1, start, endChar) || StringUtil.contains(text, end, next, startChar)) return null; if (prev + 1 < start || next > end) { return new TextRange(prev + 1, next); } return new TextRange(prev, next + 1); }