日期:2014-05-19 浏览次数:20665 次
/** * 将字符串按字节位数截取 * @param strValue 输入字符串 * @param a 截取起始位数 * @param b 截取结束位数 * @return value * @throws java.lang.Exception */ public String subStringByte(String strValue,int a,int b) throws Exception{ String value=null; byte[] bytes = new byte[2048]; System.out.println(bytes.length); bytes = strValue.getBytes(); System.out.println(bytes.length); byte[] str = new byte[b-a];//要截取的字节码 int index = 0; for(int i=a;i<b;i++) { str[index++] = bytes[i] ;//获取b-a的字节码 } value = new String(str);//转化为string return value; }
public static void main(String[] args) throws Exception { StringBuffer sb = new StringBuffer(); for(int i = 0;i < 9999;i++) { sb.append("艹x"); } System.out.println(subStringByte(sb.toString(),10,20)); } public static String subStringByte(String strValue,int a,int b) throws Exception{ String value=null; byte[] bytes = new byte[2048]; System.out.println(bytes.length); bytes = strValue.getBytes(); System.out.println(bytes.length); byte[] str = new byte[b-a];//要截取的字节码 int index = 0; for(int i=a;i<b;i++) { str[index++] = bytes[i] ;//获取b-a的字节码 } value = new String(str);//转化为string return value; }