Here you can find the source of toLength(String str, int length)
public static String toLength(String str, int length)
//package com.java2s; public class Main { public static String toLength(String str, int length) { if (str == null) { return null; }//from w w w .j a v a 2 s . c o m if (length <= 0) { return ""; } try { if (str.getBytes("GBK").length <= length) { return str; } } catch (Exception ex) { } StringBuffer buff = new StringBuffer(); int index = 0; char c; length -= 3; while (length > 0) { c = str.charAt(index); if (c < 128) { length--; } else { length--; length--; } buff.append(c); index++; } buff.append("..."); return buff.toString(); } }