public static String substringByCodepoints(String s, int fromCP, int toCP) {
        int len = s.length();
        if ((fromCP >= len) || (fromCP >= toCP)) return "";

        int from;
        try {
            from = s.offsetByCodePoints(0, fromCP);
        } catch (IndexOutOfBoundsException e) {
            return "";
        }
        if (from >= len) return "";
        int lenCP = toCP - fromCP;
        if (from + lenCP >= len) return s.substring(from);

        try {
            int to = s.offsetByCodePoints(from, toCP - fromCP);
            return (to >= len)
                    ? s.substring(from)
                    : s.substring(from, to);
        } catch (IndexOutOfBoundsException e) {
            return s.substring(from);
        }
    }
Returns a string which is never larger than the given number of code points.
If the string is longer than the given length it will be truncated.
If length is 4 or less the string will be truncated to length.
If length is longer than 4, it will be truncated at length-4 with " ..." added at the end.
vespajlib/src/main/java/com/yahoo/text/Text.java