Android examples for Internationalization:Chinese
get Chinese Length by checking byte array in UTF-8
import android.text.TextUtils; import android.util.Base64; import java.io.UnsupportedEncodingException; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class Main{ public static int getChineseLength(String src, String endcoding) { int len = 0; try {//from w w w . j av a2 s.co m int j = 0; byte[] byteValue = src.getBytes(endcoding); while (true) { short tmpst = (short) (byteValue[j] & 0xF0); if (tmpst >= 0xB0) { if (tmpst < 0xC0) { j += 2; len += 2; } else if ((tmpst == 0xC0) || (tmpst == 0xD0)) { j += 2; len += 2; } else if (tmpst == 0xE0) { j += 3; len += 2; } else if (tmpst == 0xF0) { short tmpst0 = (short) (((short) byteValue[j]) & 0x0F); if (tmpst0 == 0) { j += 4; len += 2; } else if ((tmpst0 > 0) && (tmpst0 < 12)) { j += 5; len += 2; } else if (tmpst0 > 11) { j += 6; len += 2; } } } else { j += 1; len += 1; } if (j > byteValue.length - 1) { break; } } } catch (UnsupportedEncodingException e) { return 0; } return len; } public static int getChineseLength(String src) { return getChineseLength(src, "UTF-8"); } }