您的位置:首页 > 其它

截取字符串(判断汉字)

2006-08-12 20:13 337 查看
/**
* 截取字符串 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 "";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐