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

Java按字节数截取字符串,一个汉语长度为2

2012-09-14 
Java按字节数截取字符串,一个中文长度为2碰到可能会截取汉字的情况,当然是要不能截取出乱码来,就是不能对

Java按字节数截取字符串,一个中文长度为2
碰到可能会截取汉字的情况,当然是要不能截取出乱码来,就是不能对整个汉字截取一半。如"我ABC汉字d"这个字符串,截取5个字节的时候,应该是"我ABC",而截取8个字节的时候,应该是"我ABC汉",而不应该是"我ABC汉?",其中"?"为半个汉字,可理解为向前截取

public static String subStr_1(String str, int start, int end)           throws UnsupportedEncodingException{    if (str == null)  return null;    String chinese = "[\u0391-\uFFE5]";    byte[] b = str.getBytes("UTF-8");        String temp = new String(b, start, end);    String last = getLastStr(temp);    while(!last.matches(chinese)){    temp = new String(b, start, ++end);    last = getLastStr(temp);    }        return new String(b, start, end);   }public static String getByteStr(String str, int start, int end) throws UnsupportedEncodingException{byte[] b = str.getBytes("UTF-8");return new String(b, start, end);}

热点排行