[code]public static String truncate(String source,int len,String delim){
if(source==null)
return null;
int start ,stop,byteLen;
int alen=source.getBytes().length;
if(len>0){
if(alen<=len)
return source;
start=stop=byteLen=0;
while(byteLen<=len){
if(source.substring(stop,stop+1).getBytes().length==1){
byteLen+=1;
}else{
byteLen+=2;
}
stop++;
}
StringBuffer sb=new StringBuffer(source.substring(start,stop-1));
if(alen>len)sb.append(delim);
return sb.toString();
}
return source;
}[/code]然后进行调用就可以 |