1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
public static String getSubString(String ori, int start, int end) { int index = 1; StringBuilder result = new StringBuilder();
for (char cha : ori.toCharArray()) { if (index >= start && index <= end) { result.append(cha); } if (isChineseByScript(cha) || isChinesePuctuation(cha)) { index += 2; } else { index += 1; } } return result.toString(); }
private static boolean isChineseByScript(char c) { Character.UnicodeScript sc = Character.UnicodeScript.of(c); if (sc == Character.UnicodeScript.HAN) { return true; } return false; }
private static boolean isChinesePuctuation(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS || ub == Character.UnicodeBlock.VERTICAL_FORMS) { return true; } return false; }
|