code: private static String unicodePreservingSubstring(
        String str, int begin, int end) {
      return str.substring(unicodePreservingIndex(str, begin),
          unicodePreservingIndex(str, end));
    }
docs: Returns a substring of {@code str} that respects Unicode character
  boundaries.
  
  <p>The string will never be split between a [high, low] surrogate pair,
  as defined by {@link Character#isHighSurrogate} and
  {@link Character#isLowSurrogate}.
  
  <p>If {@code begin} or {@code end} are the low surrogate of a Unicode
  character, it will be offset by -1.
  
  <p>This behavior guarantees that
  {@code str.equals(StringUtil.unicodePreservingSubstring(str, 0, n) +
  StringUtil.unicodePreservingSubstring(str, n, str.length())) } is
  true for all {@code n}.
  </pre>
  
  <p>This means that unlike {@link String#substring(int, int)}, the length of
  the returned substring may not necessarily be equivalent to
  {@code end - begin}.
  
  @param str the original String
  @param begin the beginning index, inclusive
  @param end the ending index, exclusive
  @return the specified substring, possibly adjusted in order to not
  split Unicode surrogate pairs
  @throws IndexOutOfBoundsException if the {@code begin} is negative,
  or {@code end} is larger than the length of {@code str}, or
  {@code begin} is larger than {@code end}