String (Editor editor, int startOffset, int endOffset, final boolean lower) { CharSequence text = editor.getDocument().getImmutableCharSequence(); EditorHighlighter highlighter = editor.getHighlighter(); HighlighterIterator iterator = highlighter.createIterator(startOffset); StringBuilder builder = new StringBuilder(endOffset - startOffset); while (!iterator.atEnd()) { int start = MathUtil.clamp(iterator.getStart(), startOffset, endOffset); int end = MathUtil.clamp(iterator.getEnd(), startOffset, endOffset); CharSequence fragment = text.subSequence(start, end); builder.append(iterator.getTokenType() == VALID_STRING_ESCAPE_TOKEN ? fragment : lower ? fragment.toString().toLowerCase(Locale.getDefault()) : fragment.toString().toUpperCase(Locale.getDefault())); if (end == endOffset) break; iterator.advance(); } return builder.toString(); }