Java中英文字符长度截取问题


  1. /**  
  2.      * 字符串按字节截取  
  3.      * @param str 原字符  
  4.      * @param len 截取长度  
  5.      * @return String  
  6.      * @author ivan  
  7.      * @since 2010.07.05  
  8.      */  
  9.      public static String splitString(String str, int len) {   
  10.             return splitString(str, len, "...");   
  11.      }   
  12.   
  13.      /**  
  14.       * 字符串按字节截取  
  15.       * @param str 原字符  
  16.       * @param len 截取长度  
  17.       * @param elide 省略符  
  18.       * @return String  
  19.       * @author ivan  
  20.       * @since 2010.07.05  
  21.       */  
  22.       public static String splitString(String str,int len,String elide) {   
  23.              if (str == null) {   
  24.                     return "";   
  25.              }   
  26.              byte[] strByte = str.getBytes();   
  27.              int strLen = strByte.length;   
  28.              //int elideLen = (elide.trim().length() == 0) ? 0 : elide.getBytes().length;   
  29.              int elideLen = 0;   
  30.              if (len >= strLen || len < 1) {   
  31.                     return str;   
  32.              }   
  33.              if (len - elideLen > 0) {   
  34.                     len = len - elideLen;   
  35.              }   
  36.              int count = 0;   
  37.              for (int i = 0; i < len; i++) {   
  38.                     int value = (int) strByte[i];   
  39.                     if (value < 0) {   
  40.                            count++;   
  41.                     }   
  42.              }   
  43.              if (count % 2 != 0) {   
  44.                     len = (len == 1) ? len + 1 : len - 1;   
  45.              }   
  46.              return new String(strByte, 0, len) + elide.trim();   
  47.       }  
  1. public static String subStr(String str, int num) {   
  2.         int max = num;   
  3.         try {   
  4.             max = trimGBK(str.getBytes("GBK"),num);   
  5.         } catch (UnsupportedEncodingException e) {   
  6.             e.printStackTrace();   
  7.         }     
  8.         int sum = 0;   
  9.         if (str != null && str.length() > max) {   
  10.             StringBuilder sb = new StringBuilder(max);   
  11.             for (int i = 0; i < str.length(); i++) {   
  12.                 int c = str.charAt(i);   
  13. //              if ((c & 0xff00) != 0)   
  14. //                  sum += 2;   
  15. //              else   
  16.                     sum += 1;   
  17.                 if (sum <= max)   
  18.                     sb.append((char) c);   
  19.                 else  
  20.                     break;   
  21.             }   
  22.             return sb.append("...").toString();   
  23.         } else  
  24.             return str != null ? str : "";   
  25.     }   
  26.        
  27.     public static int  trimGBK(byte[] buf,int n){     
  28.         int num = 0;     
  29.         boolean bChineseFirstHalf = false;   
  30.         if(buf.length < n )return buf.length;   
  31.         for(int i=0;i<n;i++)     
  32.         {     
  33.             if(buf[i]<0 && !bChineseFirstHalf){     
  34.                 bChineseFirstHalf = true;     
  35.             }else{     
  36.                 num++;     
  37.                 bChineseFirstHalf = false;                   
  38.             }     
  39.         }     
  40.            
  41.         return num;     
  42.     }  

相关内容