Here you can find the source of subStr(String str, int limit)
public static String subStr(String str, int limit)
//package com.java2s; //License from project: Apache License public class Main { public static String subStr(String str, int limit) { String result = str.substring(0, 17); int subLen = 17; for (int i = 0; i < limit; i++) { if (limit < getEncodingByteLen( str.substring(0, (subLen + i) > str.length() ? str.length() : (subLen)))) { result = str.substring(0, subLen + i - 1); break; }/*from ww w . ja v a 2 s . c om*/ if ((subLen + i) > str.length()) { result = str.substring(0, str.length() - 1); break; } } return result; } public static int getEncodingByteLen(String sub) { int zhLen = (sub.getBytes().length - sub.length()) * 2; int enLen = sub.length() * 2 - sub.getBytes().length; return zhLen + enLen; } }