字符串截取(中文长度为2)

StringTrimUtils,使用charArray,Java内部使用unicode,不用在意编码
java 代码
 
  1. /** 
  2.  * @author sunrie
  3.  *  
  4.  */  
  5. public class StringTrimUtils {  
  6.   
  7.     /** 
  8.      * 截取一段字符的长度(汉、日、韩文字符长度为2),不区分中英文,如果数字不正好,则少取一个字符位 
  9.      *  
  10.      * @param str 原始字符串 
  11.      * @param specialCharsLength 截取长度(汉、日、韩文字符长度为2) 
  12.      * @return 
  13.      */  
  14.     public static String trim(String str, int specialCharsLength) {  
  15.         if (str == null || "".equals(str) || specialCharsLength < 1) {  
  16.             return "";  
  17.         }  
  18.         char[] chars = str.toCharArray();  
  19.         int charsLength = getCharsLength(chars, specialCharsLength);  
  20.         return new String(chars, 0, charsLength);  
  21.     }  
  22.   
  23.     /** 
  24.      * 获取一段字符的长度,输入长度中汉、日、韩文字符长度为2,输出长度中所有字符均长度为1 
  25.      * @param chars 一段字符 
  26.      * @param specialCharsLength 输入长度,汉、日、韩文字符长度为2 
  27.      * @return 输出长度,所有字符均长度为1 
  28.      */  
  29.     private static int getCharsLength(char[] chars, int specialCharsLength) {  
  30.         int count = 0;  
  31.         int normalCharsLength = 0;  
  32.         for (int i = 0; i < chars.length; i++) {  
  33.             int specialCharLength = getSpecialCharLength(chars[i]);  
  34.             if (count <= specialCharsLength - specialCharLength) {  
  35.                 count += specialCharLength;  
  36.                 normalCharsLength++;  
  37.             } else {  
  38.                 break;  
  39.             }  
  40.         }  
  41.         return normalCharsLength;  
  42.     }  
  43.   
  44.     /** 
  45.      * 获取字符长度:汉、日、韩文字符长度为2,ASCII码等字符长度为1 
  46.      * @param c 字符 
  47.      * @return 字符长度 
  48.      */  
  49.     private static int getSpecialCharLength(char c) {  
  50.         if (isLetter(c)) {  
  51.             return 1;  
  52.         } else {  
  53.             return 2;  
  54.         }  
  55.     }  
  56.   
  57.     /** 
  58.      * 判断一个字符是Ascill字符还是其它字符(如汉,日,韩文字符) 
  59.      *  
  60.      * @param char c, 需要判断的字符 
  61.      * @return boolean, 返回true,Ascill字符 
  62.      */  
  63.     private static boolean isLetter(char c) {  
  64.         int k = 0x80;  
  65.         return c / k == 0 ? true : false;  
  66.     }  
  67. }  

SubString,网上找到的代码,加上了GBK处理,在UTF8等编码状态下也无问题
java 代码
 
  1. import java.io.UnsupportedEncodingException;  
  2.   
  3. public class SubString {  
  4.   
  5.     /** 
  6.      * 判断一个字符是Ascill字符还是其它字符(如汉,日,韩文字符) 
  7.      *  
  8.      * @param c 需要判断的字符 
  9.      * @return 返回true,Ascill字符 
  10.      */  
  11.     public static boolean isLetter(char c) {  
  12.         int k = 0x80;  
  13.         return c / k == 0 ? true : false;  
  14.     }  
  15.   
  16.     /** 
  17.      * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1 
  18.      *  
  19.      * @param s 需要得到长度的字符串 
  20.      * @return i得到的字符串长度 
  21.      */  
  22.     public static int length(String s) {  
  23.         if (s == null)  
  24.             return 0;  
  25.         char[] c = s.toCharArray();  
  26.         int len = 0;  
  27.         for (int i = 0; i < c.length; i++) {  
  28.             len++;  
  29.             if (!isLetter(c[i])) {  
  30.                 len++;  
  31.             }  
  32.         }  
  33.         return len;  
  34.     }  
  35.   
  36.     /** 
  37.      * 截取一段字符的长度,不区分中英文,如果数字不正好,则少取一个字符位 
  38.      *  
  39.      *  
  40.      * @param  origin 原始字符串 
  41.      * @param len 截取长度(一个汉字长度按2算的) 
  42.      * @param c 后缀            
  43.      * @return 返回的字符串 
  44.      */  
  45.     public static String substring(String origin, int len,String c) {  
  46.         if (origin == null || origin.equals("") || len < 1)  
  47.             return "";  
  48.         byte[] strByte = new byte[len];  
  49.         if (len > length(origin)) {  
  50.             return origin+c;  
  51.         }  
  52.         try {  
  53.             System.arraycopy(origin.getBytes("GBK"), 0, strByte, 0, len);  
  54.             int count = 0;  
  55.             for (int i = 0; i < len; i++) {  
  56.                 int value = (int) strByte[i];  
  57.                 if (value < 0) {  
  58.                     count++;  
  59.                 }  
  60.             }  
  61.             if (count % 2 != 0) {  
  62.                 len = (len == 1) ? ++len : --len;  
  63.             }  
  64.             return new String(strByte, 0, len, "GBK")+c;  
  65.         } catch (UnsupportedEncodingException e) {  
  66.             throw new RuntimeException(e);  
  67.         }  
  68.     }  
  69.   
  70. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值