Posted on 2007-04-11 08:10
skycity 阅读(1207)
评论(0) 编辑 收藏 所属分类:
J2SE技术
package net.skycity.util;
public class StringUtils extends org.apache.commons.lang.StringUtils{
public static String test(String str,int num){
char[] cs = str.toCharArray();
int count=0;
int last = cs.length;
for(int i=0;i<cs.length;i++){
if(cs[i]>255)
count+=2;
else
count++;
if(count>num){
last=i+1;
break;
}
}
if(count<num)
return str;
num -= 3;
for(int i=last-1; i>=0; i--) {
if(cs[i]>255)
count-=2;
else
count--;
if(count<=num) {
return str.substring(0, i) + "...";
}
}
return "...";
}
}
jsp调用如下:
%@page import="net.skycity.util.StringUtils"%<%=StringUtils.test("截取指定字符串长度测试",10)%>
Lyyb2001