lyyb2001

          只是為方便自己找記錄而已
          posts - 57, comments - 27, trackbacks - 0, articles - 5
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理

          JAVA截取指定字節長度的字符串

          Posted on 2007-04-11 08:10 skycity 閱讀(1220) 評論(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
          主站蜘蛛池模板: 伽师县| 万源市| 邛崃市| 长兴县| 阿巴嘎旗| 浑源县| 宁陕县| 淅川县| 屏东县| 常州市| 潜山县| 宜昌市| 乌鲁木齐县| 泸水县| 河东区| 明溪县| 青冈县| 汤原县| 长宁区| 巍山| 无锡市| 偏关县| 手游| 米脂县| 平舆县| 乳山市| 静乐县| 铜梁县| 齐齐哈尔市| 高安市| 文山县| 南丰县| 彰化市| 长沙县| 青铜峡市| 明水县| 莆田市| 贵州省| 乌鲁木齐市| 太和县| 曲阜市|