Posted on 2007-04-11 08:10
skycity 閱讀(1213)
評論(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