有一文本,每行的格式为:0123456789张三male   10000.00
现在我读出这个字符串,但想取得male这个值的时候由于"张三"这个汉字占位问题,我用subString(14,17)的时候,只会得到"le",也就是说,本来应该是用subString(12,15)可以得到"male",但"张三"在文本中占了四个字节,用subString读的时候只用两个字节,不知道有什么办法能够保证,我取"male"这个列的数据的时候能够正确得到.我试过用转码,可以得到到正确结果,但同时汉字会出现乱码.

解决方案 »

  1.   

    我以前用两个工具方法来实现按字节截取,方法如下: /**
     * 按字节截取字符串子串
     * @param value
     * @param length
     * @return
     */
    public static String leftOfByte(String value,int length) {
    if (value == null||value.equals("")){
    return "";
    }
    String temp;
    int yy = length;
    if (value.length()<length){
    temp = value;
    yy = temp.length();
    }else{
    temp = value.substring(0,length);
    } int xx = lengthOfByte(temp,2) - length; while (xx>0){
    if (xx==1){
    yy = yy-xx;
    }else{
    yy = yy-xx/2;
    } temp = temp.substring(0,yy);
    xx = lengthOfByte(temp,2) - length;
    } return temp;
    } /**
     * 获得字符串的字节长度
     * @param value 测量字符串
     * @param cLength 一个中文字符等于多少字节,如果是0则让系统计算
     * @return
     */
    public static int lengthOfByte(String value,int cLength) {
    if (value == null|| value.equals(""))
    return 0; StringBuffer buff = new StringBuffer(value);
    int length = 0;
    String stmp;
    for (int i = 0; i < buff.length(); i++) {
    stmp = buff.substring(i, i + 1); try {
    stmp = new String(stmp.getBytes("utf8"));
    } catch (Exception e) {} if (stmp.getBytes().length > 1) {
    if(cLength==0){
    length += stmp.getBytes().length;
    }else{
    length += cLength;
    } } else {
    length += 1;
    }
    }
    return length;
    }你可以参考这两个方法来实现你要的截取
      

  2.   

    可以试试把字符串从空白处(“ ”)截断 即str1=0123456789张三male str2=10000.00 
    然后取str1的后4位即 male
      

  3.   

    TO:eric0cn
    您的意思是用leftOfByte方法来代替subString方法吗?
    也就是我用这个方法的时候参数是怎样传的?
    String value="0123456789张三male   10000.00";
    leftOfByte(value,?),您能明确说一下怎样使用吗?TO:musiclee
    这个方法不太可取,因为在分开str1和str2的时候就没有办法定位了
      

  4.   

    临时改了个,你看看行不行:
    /**
     * 按字节截取字符串
     * @param value 原字符串
     * @param begin 开始位置
     * @param length 截取长度
     * @return
     */
    public static String subStringOfByte(String value,int begin,int length) {
    if (value == null||value.equals("")){
    return "";
    } if (lengthOfByte(value,2) <= begin){
    return "";
    }

    String temp;
    StringBuffer sb = new StringBuffer();

    int xx = lengthOfByte(sb.toString(),2) - length;
    int yy = 0;
    int zz = 0;

    while (xx<0){
    temp = value.substring(yy, yy+1);
    yy++;
    zz = zz + lengthOfByte(temp,2);
    if (zz>begin){
    sb.append(temp);
    } xx = lengthOfByte(sb.toString(),2) - length;
    } return sb.toString();
    }
    /**
     * 获得字符串的字节长度
     * @param value 测量字符串
     * @param cLength 一个中文字符等于多少字节,如果是0则让系统计算
     * @return
     */
    public static int lengthOfByte(String value,int cLength) {
    if (value == null|| value.equals(""))
    return 0; StringBuffer buff = new StringBuffer(value);
    int length = 0;
    String stmp;
    for (int i = 0; i < buff.length(); i++) {
    stmp = buff.substring(i, i + 1); try {
    stmp = new String(stmp.getBytes("utf8"));
    } catch (Exception e) {} if (stmp.getBytes().length > 1) {
    if(cLength==0){
    length += stmp.getBytes().length;
    }else{
    length += cLength;
    } } else {
    length += 1;
    }
    }
    return length;
    }System.out.println(subStringOfByte("0123456789张三male   10000.00 ",14,4));
      

  5.   

    问题已解决,用的还是用字节流Byte数据的方式做的,共享一下思路:
    byte[] 大数组
    循环赋值,把子byte[] toString一下,就OK了.
    非常感谢.