1. 程式人生 > >Java的中英文混合擷取字串的問題解決方法

Java的中英文混合擷取字串的問題解決方法

 Java預設的獲取一個字元的長度(不管是否是中英文)都是作為一個位元組來處理,但是插入資料庫(Oracle)時中文卻佔了2個位元組,

這點搞得人很苦惱。在網上搜索時,找到兩種好的解決方案,這裡進行了修改,程式碼如下:

package com.core.utils;

import java.io.UnsupportedEncodingException;
import org.apache.commons.lang.StringUtils;


public class StringSubStrUtils {
 
   /** 
    * 擷取一段字元的長度,不區分中英文,如果數字不正好,則少取一個字元位 
    * @param origin 原始字串 
    * @param begin 擷取開始位置(一個漢字長度按2算的)
    * @param end 擷取結束位置(一個漢字長度按2算的) 
    * @param appendStr 待新增字串  
    * @param encoding 編碼格式,預設GBK         
    * @return 返回的字串 
    */  
   public static String substring

(String origin,int begin,int end,String appendStr,String encoding) {  
       if (origin == null || origin.equals(""))  
       {
           return appendStr;
       }
       if(begin<0)
       {
        begin=0;
       }
       if(end<0)
       {
        return "";
       }      
       if(begin>end)
       {
        return "";
       }
       if(begin==end)
       {
        return "";
       }
       if(begin>length(origin))
       {
        return "";
       }
       if(end>length(origin))
       {
        end=length(origin)-1;
       }      
       if(StringUtils.isBlank(encoding) || StringUtils.isEmpty(encoding))
       {
        encoding="GBK";
       }
       int len=end-begin;      
       byte[] strByte = new byte[len];     
       try {  
           System.arraycopy(origin.getBytes(encoding),begin, strByte, 0, len);  
           int count = 0;  
           for (int i = 0; i < len; i++) {  
               int value = (int) strByte[i];  
               if (value < 0) {  
                   count++;  
               }  
           }         
           if (count % 2 != 0) {  
               len = (len == 1) ? ++len : --len;  
           }         
           return new String(strByte, 0, len, encoding) + appendStr;  
       }
       catch (UnsupportedEncodingException e) {  
           throw new RuntimeException(e);  
       }catch(StringIndexOutOfBoundsException ex)
       {
        return appendStr;
       }
       catch(Exception ex)
       {
        return "";
       }      
   }  
  
   /** 
    * 判斷一個字元是Ascill字元還是其它字元(如漢,日,韓文字元) 
    *  
    * @param c 需要判斷的字元 
    * @return 返回true,Ascill字元 
    */  
   public static boolean isLetter(char c) {  
       int k = 0x80;  
       return c / k == 0 ? true : false;  
   }  
 
   /** 
    * 得到一個字串的長度,顯示的長度,一個漢字或日韓文長度為2,英文字元長度為1 
    *  
    * @param s 需要得到長度的字串 
    * @return i得到的字串長度 
    */  
   public static int length(String s) {  
       if (s == null)  
       {
           return 0;
       }
       char[] c = s.toCharArray();  
       int len = 0;  
       for (int i = 0; i < c.length; i++) {  
           len++;  
           //如果為漢,日,韓,則多加一位
           if (!isLetter(c[i])) {  
               len++;  
           }  
       }  
       return len;  
   }  
  


   /******************************* Second Method ****************************/
  
   /** 
    * 擷取一段字元的長度(漢、日、韓文字元長度為2),不區分中英文,如果數字不正好,則少取一個字元位 
    *  
    * @param str 原始字串 
    * @param srcPos 開始位置
    * @param specialCharsLength 擷取長度(漢、日、韓文字元長度為2) 
    * @return 
    */  
   public static String substring

(String str, int srcPos, int specialCharsLength) {  
       if (str == null || "".equals(str) || specialCharsLength < 1) {  
           return "";  
       }
       if(srcPos<0)
       {
        srcPos=0;
       }
       if(specialCharsLength<=0)
       {
        return "";
       }
       //獲得字串的長度
       char[] chars = str.toCharArray();
       if(srcPos>chars.length)
       {
        return "";
       }     
       int charsLength = getCharsLength(chars, specialCharsLength);      
       return new String(chars, srcPos, charsLength);  
   }  
 
   /** 
    * 獲取一段字元的長度,輸入長度中漢、日、韓文字元長度為2,輸出長度中所有字元均長度為1 
    * @param chars 一段字元 
    * @param specialCharsLength 輸入長度,漢、日、韓文字元長度為2 
    * @return 輸出長度,所有字元均長度為1 
    */  
   private static int getCharsLength(char[] chars, int specialCharsLength) {   
       int count = 0;  
       int normalCharsLength = 0;  
       for (int i = 0; i < chars.length; i++) {  
           int specialCharLength = getSpecialCharLength(chars[i]);          
           if (count <= specialCharsLength - specialCharLength) {           
               count += specialCharLength;  
               normalCharsLength++;  
           } else {  
               break;  
           }  
       }      
       return normalCharsLength;  
   }  
 
   /** 
    * 獲取字元長度:漢、日、韓文字元長度為2,ASCII碼等字元長度為1 
    * @param c 字元 
    * @return 字元長度 
    */  
   private static int getSpecialCharLength(char c) {  
       if (isLetter(c)) {  
           return 1;  
       } else {  
           return 2;  
       }  
   }  
 
  
   public static void main(String[] args)
   {
      String str="中國I love you!哈哈.";
      System.out.println(substring(str,15,17,"---擷取字串測試","GBK"));  
   
      str="武漢市洪山區 HuaZhong Technogy College 武昌分校";
      System.out.println(substring(str,-1,100));   
   }

}