qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

Java精确截取字符串

 Java精确截取字符串,取得字符串前面指定长度字符函数

  用java取得字符串的前面部分内容的函数contentStr = contenttemp.substring(0, 150);其中要保证最大长度不能超过字符串的长度。下面是我的实现部分代码,以及网上搜索的相关代码:

  • /* 
  • * content内容过长可能会导致xml文件过大,加载太慢。 
  • * 但从seo的角度考虑全部输出有利于搜索引擎,但一般情况下内容也不会太多 
  • * 为防止空格换行css无法控制撑大页面,用正则表达式替换掉空格,所以截取前面100个字符,页面显示的内容多少用css控制 
  •  *zdz的作品,流风的作品 
  • */ 
  • //str.trim().replaceAll("\\s+"," ");  
  • String contenttemp = rs.getString(contentName).trim().replaceAll("\\s+","");  
  • //NpfDebug.print(contenttemp.length()); 
  • if(contenttemp.length()>100){//如果长度大于100则截取 
  •  contenttemp = contenttemp.substring(0100);  
  •  //NpfDebug.print("contenttemp.length()>100 ? "+contenttemp.length()+"\n"+contentStr); 
  • }  
  • rsbody.append(beforCONTENT);  
  • rsbody.append(contenttemp);  
  • rsbody.append(endCONTENT);
  •   开发中经常遇到,字符串过长,无法完全显示的问题

      这时候就需要截取我们所需要的长度,后面显示省略号或其他字符。

      由于中文字符占两个字节,而英文字符占用一个字节,所以,单纯地判断字符数,效果往往不尽如人意

      下面的方法通过判断字符的类型来进行截取,效果还算可以:)

      如果大家有其他的解决方法欢迎贴出来,共同学习:)

  • private String str;  
  • private int counterOfDoubleByte;  
  • private byte b[];  
  • /** 
  • * 设置需要被限制长度的字符串 
  • * @param str 需要被限制长度的字符串 
  • */ 
  • public void setLimitLengthString(String str){  
  •    this.str = str;  
  • }  
  • /** 
  • * @param len 需要显示的长度(<font color="red">注意:长度是以byte为单位的,一个汉字是2个byte</font>) 
  • * @param symbol 用于表示省略的信息的字符,如“...”,“>>>”等。 
  • * @return 返回处理后的字符串 
  • */ 
  • public String getLimitLengthString(int len, String symbol) throws UnsupportedEncodingException {  
  •    counterOfDoubleByte = 0;  
  •    b = str.getBytes("GBK");  
  •    if(b.length <= len)  
  •      return str;  
  •    for(int i = 0; i < len; i++){  
  •      if(b[i] < 0)  
  •        counterOfDoubleByte++;  
  •    }  
  •    if(counterOfDoubleByte % 2 == 0)  
  •      return new String(b, 0, len, "GBK") + symbol;  
  •    else 
  •      return new String(b, 0, len - 1"GBK") + symbol;  
  • }  
  •    
  •    
  •    
  • -------------------  
  •    
  • /** *//** 
  •     * 按字节长度截取字符串 
  •     * @param str 将要截取的字符串参数 
  •     * @param toCount 截取的字节长度 
  •     * @param more 字符串末尾补上的字符串 
  •     * @return 返回截取后的字符串 
  •     */ 
  •    public String substring(String str, int toCount, String more) ...{  
  •      int reInt = 0;  
  •      String reStr = "";  
  •      if (str == null)  
  •        return "";  
  •      char[] tempChar = str.toCharArray();  
  •      for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) ...{  
  •        String s1 = str.valueOf(tempChar[kk]);  
  •        byte[] b = s1.getBytes();  
  •        reInt += b.length;  
  •        reStr += tempChar[kk];  
  •      }  
  •      if (toCount == reInt || (toCount == reInt - 1))  
  •        reStr += more;  
  •      return reStr;  
  •    }  
  •    
  • =================  
  •    
  • /** 
  •      * 取字符串的前toCount个字符 
  •      * 
  •      * @param str 被处理字符串 
  •      * @param toCount 截取长度 
  •      * @param more 后缀字符串 
  •      * @version 2004.11.24 
  •      * @author zhulx 
  •      * @return String 
  •      */ 
  •     public static String substring(String str, int toCount,String more)  
  •     {  
  •       int reInt = 0;  
  •       String reStr = "";  
  •       if (str == null)  
  •         return "";  
  •       char[] tempChar = str.toCharArray();  
  •       for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) {  
  •         String s1 = str.valueOf(tempChar[kk]);  
  •         byte[] b = s1.getBytes();  
  •         reInt += b.length;  
  •         reStr += tempChar[kk];  
  •       }  
  •       if (toCount == reInt || (toCount == reInt - 1))  
  •         reStr += more;  
  •       return reStr;  
  •     }
  •   得到字符串真实长度和取固定长度的字符串函数

  • // 截取固定长度子字符串 sSource为字符串iLen为长度  
  • function getInterceptedStr(sSource, iLen)   
  • {   
  •     if(sSource.replace(/[^\x00-\xff]/g,"xx").length <= iLen)   
  •     {   
  •         return sSource;   
  •     }   
  •     var ELIDED = "";   
  •        
  •     var str = "";   
  •     var l = 0;   
  •     var schar;   
  •     for(var i=0; schar=sSource.charAt(i); i++)   
  •     {   
  •         str += schar;   
  •         l += (schar.match(/[^\x00-\xff]/) != null ? 2 : 1);   
  •         if(l >= iLen - ELIDED.length)   
  •         {   
  •             break;   
  •         }   
  •     }   
  •     str += ELIDED;   
  •        
  •     return str;   
  • }
  • posted on 2011-12-22 11:12 顺其自然EVO 阅读(299) 评论(0)  编辑  收藏


    只有注册用户登录后才能发表评论。


    网站导航:
     
    <2011年12月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    导航

    统计

    常用链接

    留言簿(55)

    随笔分类

    随笔档案

    文章分类

    文章档案

    搜索

    最新评论

    阅读排行榜

    评论排行榜