/* * String.valueOf(arr[i]).matches("[\u4e00-\u9fa5]") 只是汉字 * String.valueOf(arr[i]).matches("[^x00-xff]") 双字节(包括汉字) */ public static String getStrBylength(String str , int len) { if(null == str) return ""; int sl = str.getBytes().length; if(sl>len*2) { StringBuffer sb = new StringBuffer(); char[] arr = str.toCharArray(); for (int i = 0,j=0; i<arr.length && j<len*2; i++) { if (String.valueOf(arr[i]).matches("[^x00-xff]")) { j+=2; } else { j++; } if(j==len*2-1 && String.valueOf(arr[i]).matches("[^x00-xff]")) { } else { sb.append(arr[i]); } } return sb.toString()+"..."; } else { return str; } }
(责任编辑:admin) |