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

jsp截取中文字符串 len为字节长度

复制代码 代码如下:

/**
* 截取字符串 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 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 "";
}
}

以上就是java 截取字符串(判断汉字)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java