Here you can find the source of subStringChinese(String str, int startIndex, int endIndex)
public static String subStringChinese(String str, int startIndex, int endIndex)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { public static String subStringChinese(String str, int startIndex, int endIndex) { int length = 0; int size = endIndex - startIndex; List<Character> charList = new ArrayList<Character>(); for (char c : str.toCharArray()) { if (isChinese(c)) length += 2;/*from w w w. ja v a2 s . c om*/ else length++; charList.add(c); if (length >= size) break; } StringBuilder builder = new StringBuilder(); for (Character c : charList) { builder.append(c); } return builder.toString(); } public static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { return true; } return false; } }