qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java精確截取字符串

           Java精確截取字符串,取得字符串前面指定長度字符函數

            用java取得字符串的前面部分內容的函數contentStr = contenttemp.substring(0, 150);其中要保證最大長度不能超過字符串的長度。下面是我的實現部分代碼,以及網上搜索的相關代碼:

        1. /* 
        2. * content內容過長可能會導致xml文件過大,加載太慢。 
        3. * 但從seo的角度考慮全部輸出有利于搜索引擎,但一般情況下內容也不會太多 
        4. * 為防止空格換行css無法控制撐大頁面,用正則表達式替換掉空格,所以截取前面100個字符,頁面顯示的內容多少用css控制 
        5.  *zdz的作品,流風的作品 
        6. */ 
        7. //str.trim().replaceAll("\\s+"," ");  
        8. String contenttemp = rs.getString(contentName).trim().replaceAll("\\s+","");  
        9. //NpfDebug.print(contenttemp.length()); 
        10. if(contenttemp.length()>100){//如果長度大于100則截取 
        11.  contenttemp = contenttemp.substring(0100);  
        12.  //NpfDebug.print("contenttemp.length()>100 ? "+contenttemp.length()+"\n"+contentStr); 
        13. }  
        14. rsbody.append(beforCONTENT);  
        15. rsbody.append(contenttemp);  
        16. rsbody.append(endCONTENT);
        17.   開發中經常遇到,字符串過長,無法完全顯示的問題

            這時候就需要截取我們所需要的長度,后面顯示省略號或其他字符。

            由于中文字符占兩個字節,而英文字符占用一個字節,所以,單純地判斷字符數,效果往往不盡如人意

            下面的方法通過判斷字符的類型來進行截取,效果還算可以:)

            如果大家有其他的解決方法歡迎貼出來,共同學習:)

        18. private String str;  
        19. private int counterOfDoubleByte;  
        20. private byte b[];  
        21. /** 
        22. * 設置需要被限制長度的字符串 
        23. * @param str 需要被限制長度的字符串 
        24. */ 
        25. public void setLimitLengthString(String str){  
        26.    this.str = str;  
        27. }  
        28. /** 
        29. * @param len 需要顯示的長度(<font color="red">注意:長度是以byte為單位的,一個漢字是2個byte</font>) 
        30. * @param symbol 用于表示省略的信息的字符,如“...”,“>>>”等。 
        31. * @return 返回處理后的字符串 
        32. */ 
        33. public String getLimitLengthString(int len, String symbol) throws UnsupportedEncodingException {  
        34.    counterOfDoubleByte = 0;  
        35.    b = str.getBytes("GBK");  
        36.    if(b.length <= len)  
        37.      return str;  
        38.    for(int i = 0; i < len; i++){  
        39.      if(b[i] < 0)  
        40.        counterOfDoubleByte++;  
        41.    }  
        42.    if(counterOfDoubleByte % 2 == 0)  
        43.      return new String(b, 0, len, "GBK") + symbol;  
        44.    else 
        45.      return new String(b, 0, len - 1"GBK") + symbol;  
        46. }  
        47.    
        48.    
        49.    
        50. -------------------  
        51.    
        52. /** *//** 
        53.     * 按字節長度截取字符串 
        54.     * @param str 將要截取的字符串參數 
        55.     * @param toCount 截取的字節長度 
        56.     * @param more 字符串末尾補上的字符串 
        57.     * @return 返回截取后的字符串 
        58.     */ 
        59.    public String substring(String str, int toCount, String more) ...{  
        60.      int reInt = 0;  
        61.      String reStr = "";  
        62.      if (str == null)  
        63.        return "";  
        64.      char[] tempChar = str.toCharArray();  
        65.      for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) ...{  
        66.        String s1 = str.valueOf(tempChar[kk]);  
        67.        byte[] b = s1.getBytes();  
        68.        reInt += b.length;  
        69.        reStr += tempChar[kk];  
        70.      }  
        71.      if (toCount == reInt || (toCount == reInt - 1))  
        72.        reStr += more;  
        73.      return reStr;  
        74.    }  
        75.    
        76. =================  
        77.    
        78. /** 
        79.      * 取字符串的前toCount個字符 
        80.      * 
        81.      * @param str 被處理字符串 
        82.      * @param toCount 截取長度 
        83.      * @param more 后綴字符串 
        84.      * @version 2004.11.24 
        85.      * @author zhulx 
        86.      * @return String 
        87.      */ 
        88.     public static String substring(String str, int toCount,String more)  
        89.     {  
        90.       int reInt = 0;  
        91.       String reStr = "";  
        92.       if (str == null)  
        93.         return "";  
        94.       char[] tempChar = str.toCharArray();  
        95.       for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) {  
        96.         String s1 = str.valueOf(tempChar[kk]);  
        97.         byte[] b = s1.getBytes();  
        98.         reInt += b.length;  
        99.         reStr += tempChar[kk];  
        100.       }  
        101.       if (toCount == reInt || (toCount == reInt - 1))  
        102.         reStr += more;  
        103.       return reStr;  
        104.     }
        105.   得到字符串真實長度和取固定長度的字符串函數

        106. // 截取固定長度子字符串 sSource為字符串iLen為長度  
        107. function getInterceptedStr(sSource, iLen)   
        108. {   
        109.     if(sSource.replace(/[^\x00-\xff]/g,"xx").length <= iLen)   
        110.     {   
        111.         return sSource;   
        112.     }   
        113.     var ELIDED = "";   
        114.        
        115.     var str = "";   
        116.     var l = 0;   
        117.     var schar;   
        118.     for(var i=0; schar=sSource.charAt(i); i++)   
        119.     {   
        120.         str += schar;   
        121.         l += (schar.match(/[^\x00-\xff]/) != null ? 2 : 1);   
        122.         if(l >= iLen - ELIDED.length)   
        123.         {   
        124.             break;   
        125.         }   
        126.     }   
        127.     str += ELIDED;   
        128.        
        129.     return str;   
        130. }
        131. posted on 2011-12-22 11:12 順其自然EVO 閱讀(306) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2011年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南丹县| 鹿邑县| 开鲁县| 成武县| 茂名市| 海盐县| 大姚县| 宁陕县| 徐州市| 遂昌县| 乐至县| 民勤县| 龙游县| 微山县| 信宜市| 崇州市| 丹棱县| 三门峡市| 双峰县| 平果县| 宝丰县| 泗洪县| 文水县| 昭通市| 三明市| 莫力| 石阡县| 安达市| 昌都县| 乌海市| 阳谷县| 辽宁省| 巢湖市| 海南省| 林西县| 盐边县| 安康市| 青河县| 新平| 肇东市| 上饶市|