Here you can find the source of truncate(String strOrginal, int iByteMaxSize)
public static final String truncate(String strOrginal, int iByteMaxSize)
//package com.java2s; //License from project: Open Source License public class Main { public static final String truncate(String strOrginal, int iByteMaxSize) { int iByteLen = 0; final int strLen = strOrginal.length(); if (iByteMaxSize < 4) { return "...".substring(0, iByteMaxSize); }//from www . ja v a 2 s . co m for (int i = 0; i < strLen; i++) { iByteLen += ((strOrginal.charAt(i) & 0xFF00) == 0) ? 1 : 2; if (iByteLen > iByteMaxSize) { for (; iByteLen > (iByteMaxSize - 3); i--) { iByteLen -= ((strOrginal.charAt(i) & 0xFF00) == 0) ? 1 : 2; } // for return strOrginal.substring(0, i + 1) + "..."; } // if } // for return strOrginal; } }