java 截取字符串(判断汉字)
发布时间:2020-12-14 05:35:42 所属栏目:Java 来源:网络整理
导读:复制代码 代码如下: /** * 截取字符串 len为字节长度 * @param str * @param len * @return * @throws UnsupportedEncodingException */ public static String getLimitLengthString( String str,int len){ try{ int counterOfDoubleByte = 0; byte[] b = st
复制代码 代码如下: /** * 截取字符串 len为字节长度 * @param str * @param len * @return * @throws UnsupportedEncodingException */ public static String getLimitLengthString( String str,int len){ try{ int counterOfDoubleByte = 0; byte[] b = str.getBytes("gb2312"); if(b.length <= len) return str; for(int i = 0; i < len; i++){ if(b[i] < 0) counterOfDoubleByte++; } if(counterOfDoubleByte % 2 == 0) return new String(b,len,"gb2312"); else return new String(b,len - 1,"gb2312"); }catch(Exception ex){ DBTools.error(ex); return ""; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |