当前位置:主页 > 查看内容

java 截取字符串(判断汉字)

发布时间:2021-07-17 00:00| 位朋友查看

简介:复制代码 代码如下: /** * 截取字符串 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, 0, len, "gb2312");
else
return new String(b, 0, len - 1, "gb2312");
}catch(Exception ex){
DBTools.error(ex);
return "";
}
}


本文转载自网络,原文链接:https://m.jb51.net/article/16560.htm
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐