用Java实现按字节长度截取中英文数字字符串的方法总结

方法一

//jdk1.4.2.05

Java代码   收藏代码
  1. /** 
  2.  * @author cn 
  3.  * @param s 要截取的字符串 
  4.  * @param length 要截取字符串的长度->是字节一个汉字2个字节 
  5.  * return 返回length长度的字符串(含汉字) 
  6. */  
  7. private static String getTitleToTen(String s, int length) throws Exception  
  8.    {  
  9.   
  10.        byte[] bytes = s.getBytes("Unicode");  
  11.        int n = 0;  
  12.        int i = 2;  
  13.        for (; i < bytes.length && n < length; i++){  
  14.         if (i % 2 == 0){  
  15.                n++;  
  16.            }else{  
  17.                if (bytes[i] != 0){  
  18.                    n++;  
  19.                }  
  20.            }  
  21.        }  
  22.        /*if (i % 2 == 1){ 
  23.            if (bytes[i - 1] == 0) 
  24.                i = i - 1; 
  25.            else 
  26.                i = i + 1; 
  27.        }*/  
  28.        //将截一半的汉字要保留  
  29.        if (i % 2 == 1){  
  30.         i = i + 1;  
  31.        }  
  32.        String eside = ".................................................................";  
  33.        byte[] byteEside = eside.getBytes("Unicode");  
  34.        String title = "";  
  35.        if (bytes[i-1] == 0){  
  36.         title = new String(bytes, 0, i, "Unicode")+new String(byteEside,0,40,"Unicode");  
  37.        }else{  
  38.         title = new String(bytes, 0, i, "Unicode")+new String(byteEside,0,38,"Unicode");  
  39.        }  
  40.        return title;  
  41.    }  

 //方法改进

Java代码   收藏代码
  1. /** 
  2.      * @author cn 
  3.      * @param s 要截取的字符串 
  4.      * @param length 要截取字符串的长度->是字节一个汉字2个字节 
  5.      * return 返回length长度的字符串(含汉字) 
  6.     */  
  7.     private static String getTitleToTen(String s, int length) throws Exception  
  8.     {  
  9.   
  10.         String title = "";  
  11.         s = s.trim();  
  12.         byte[] bytes = s.getBytes("Unicode");  
  13.         int n = 0;  
  14.         int i = 2;  
  15.         int chineseNum = 0;  
  16.         int englishNum = 0;  
  17.         for (; i < bytes.length && n < length; i++){  
  18.             if (i % 2 == 0){  
  19.                 n++;  
  20.             }else{  
  21.                 if (bytes[i] != 0){  
  22.                     n++;  
  23.                     chineseNum++;  
  24.                 }else{  
  25.                     englishNum++;  
  26.                 }  
  27.             }  
  28.         }  
  29.         /*if (i % 2 == 1){ 
  30.             if (bytes[i - 1] == 0) 
  31.                 i = i - 1; 
  32.             else 
  33.                 i = i + 1; 
  34.         }*/  
  35.         //将截一半的汉字要保留  
  36.         if (i % 2 == 1){  
  37.             i = i + 1;  
  38.         }  
  39.     //最后一个为非汉字则英文字符加一  
  40.         if (bytes[i-1] == 0){  
  41.             englishNum++;  
  42.                  
  43.         }else if (englishNum % 2 != 0){//如果英文字符mod 2 != 0 代表有奇数个英文字符,所以汉字个数加一  
  44.             chineseNum++;  
  45.         }  
  46.         String eside = ".................................................................";  
  47.         String str = new String(bytes,0,i,"Unicode");  
  48.         StringBuffer ssss = new StringBuffer(str);  
  49.         ssss.append(eside);  
  50.         byte[] byteTitle = ssss.toString().getBytes("Unicode");  
  51.         int lll = (length*4-4)-2*chineseNum;//length截取字符串字节数(length*2)*(length*2)[length*2]代表参数s,和length转换成bytes[] 的字节数  
  52.         title = new String(byteTitle,0,lll,"Unicode");  
  53.         return title;  
  54.     }  

 

  

   //jdk1.6.0.06

Java代码   收藏代码
  1. /** 
  2.  * @author cn 
  3.  * @param s 要截取的字符串 
  4.  * @param length 要截取字符串的长度->是字节一个汉字2个字节 
  5.  * return 返回length长度的字符串(含汉字) 
  6. */  
  7.    public static String bSubstring(String s, int length) throws Exception  
  8.    {  
  9.   
  10.        byte[] bytes = s.getBytes("Unicode");  
  11.        int n = 0// 表示当前的字节数  
  12.        int i = 2// 要截取的字节数,从第3个字节开始  
  13.        for (; i < bytes.length && n < length; i++){  
  14.            // 奇数位置,如3、5、7等,为UCS2编码中两个字节的第二个字节  
  15.            if (i % 2 == 1){  
  16.                n++; // 在UCS2第二个字节时n加1  
  17.            }  
  18.            else{  
  19.                // 当UCS2编码的第一个字节不等于0时,该UCS2字符为汉字,一个汉字算两个字节  
  20.                if (bytes[i] != 0){  
  21.                    n++;  
  22.                }  
  23.            }  
  24.              
  25.        }  
  26.        // 如果i为奇数时,处理成偶数  
  27.        /*if (i % 2 == 1){ 
  28.            // 该UCS2字符是汉字时,去掉这个截一半的汉字 
  29.            if (bytes[i - 1] != 0) 
  30.                i = i - 1; 
  31.            // 该UCS2字符是字母或数字,则保留该字符 
  32.            else 
  33.                i = i + 1; 
  34.        }*/  
  35.        //将截一半的汉字要保留  
  36.        if (i % 2 == 1){  
  37.         i = i + 1;  
  38.        }  
  39.        return new String(bytes, 0, i, "Unicode");  
  40.    }  

 

 

 

方法二

Java代码   收藏代码
  1. package proc;  
  2.   
  3. public class Tools {  
  4.      public Tools() {     
  5.      }      
  6.     /**  
  7.       * 字符串按字节截取  
  8.       * @param str 原字符  
  9.       * @param len 截取长度  
  10.       * @return String  
  11.       * @author kinglong  
  12.       * @since 2006.07.20  
  13.       */     
  14.      public static String splitString(String str, int len) {     
  15.             return splitString(str, len, ".......");     
  16.       }     
  17.     
  18.      /**  
  19.        * 字符串按字节截取  
  20.        * @param str 原字符  
  21.        * @param len 截取长度  
  22.        * @param elide 省略符  
  23.        * @return String  
  24.        * @author kinglong  
  25.        * @since 2006.07.20  
  26.        */     
  27.       public static String splitString(String str,int len,String elide) {     
  28.              if (str == null) {     
  29.                     return "";     
  30.               }     
  31.              byte[] strByte = str.getBytes();     
  32.              int strLen = strByte.length;     
  33.              //int elideLen = (elide.trim().length() == 0) ? 0 : elide.getBytes().length;     
  34.              if (len >= strLen || len < 1) {     
  35.                     return str;     
  36.               }     
  37.             /* if (len - elideLen > 0) {    
  38.                      len = len - elideLen;    
  39.               }  */   
  40.              int count = 0;     
  41.              for (int i = 0; i < len; i++) {     
  42.                     int value = (int) strByte[i];     
  43.                     if (value < 0) {     
  44.                             count++;     
  45.                      }     
  46.               }     
  47.              if (count % 2 != 0) {     
  48.                      len = (len == 1) ? len + 1 : len - 1;     
  49.               }     
  50.              return new String(strByte, 0, len) + elide.trim();     
  51.        }    
  52.       /** 
  53.      * @param args 
  54.      */  
  55.     public static void main(String[] args) {  
  56.         // TODO Auto-generated method stub  
  57.         Tools cs = new Tools();  
  58.         //String s = "a加b等cc于c";  
  59.         //String s = "a加b等cc于c";  
  60.         String s ="aaas学位英语专区 学s位英语专区 学s位英语专区 学位英语专区";   
  61.         try{  
  62.             System.out.println(cs.splitString(s, 20));  
  63.         }catch(Exception e){  
  64.             e.printStackTrace();  
  65.         }  
  66.     }  
  67. }   

 

方法三

Java代码   收藏代码
  1. /** 
  2.        * 取字符串的前toCount个字符 
  3.        * 
  4.        * @param str 被处理字符串 
  5.        * @param toCount 截取长度 
  6.        * @param more 后缀字符串 
  7.        * @version 2004.11.24 
  8.        * @author zhulx 
  9.        * @return String 
  10.        */  
  11.         public static String substring(String str, int toCount,String more)throws Exception{  
  12.             int reInt = 0;  
  13.             String reStr = "";  
  14.             if (str == nullreturn "";  
  15.             char[] tempChar = str.toCharArray();  
  16.             for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) {  
  17.                 String s1 = str.valueOf(tempChar[kk]);  
  18.                 byte[] b = s1.getBytes();  
  19.                 reInt += b.length;  
  20.                 reStr += tempChar[kk];  
  21.             }  
  22.             if (toCount == reInt || (toCount == reInt - 1))  
  23.                 reStr += more;  
  24.             return reStr;  
  25.             }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值