首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

ava字符串长度截取 思忖汉字

2012-12-21 
ava字符串长度截取 考虑汉字public static String getLimitLengthString( String str,int len){try{int co

ava字符串长度截取 考虑汉字

 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 "";      }      }

热点排行