为什么我的string长度明明够,但是substring总是出错啊?有高手指点一下吗?

解决方案 »

  1.   

    不会吧
    str=str.subString(ints,inte);
    我用过,不会有问题。
      

  2.   

    out.print(dataRec);
      String str;
      str = dataRec.substring(6,4);
      
    明明dataRec是十五个字符,怎么会出错呢?
      

  3.   

    public String substring(int beginIndex,
                            int endIndex)
    Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. 
    Examples:  "hamburger".substring(4, 8) returns "urge"
     "smiles".substring(1, 5) returns "mile"
     Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive. 
    注意:第二个参数并不是截取的子串的长度,而是要截取的终止位置.
      

  4.   

    out.print(dataRec);
      String str;
      str = dataRec.substring(6,4);
    如果按你说的是没错。
    我想问题还是在dataRec里
    你能保证有15位吗?
    如果是真的那就是JDK的问题。
      

  5.   

    substring返回位于 String 对象中指定位置的子字符串。 strVariable.substring(start, end)
    "String Literal".substring(start, end)参数
    start指明子字符串的起始位置,该索引从 0 开始起算。end指明子字符串的结束位置,该索引从 0 开始起算。说明
    substring 方法将返回一个包含从 start 到最后(不包含 end )的子字符串的字符串。substring 方法使用 start 和 end 两者中的较小值作为子字符串的起始点。例如, strvar.substring(0, 3) 和 strvar.substring(3, 0) 将返回相同的子字符串。 如果 start 或 end 为 NaN 或者负数,那么将其替换为0。 子字符串的长度等于 start 和 end 之差的绝对值。例如,在 strvar.substring(0, 3) 和 strvar.substring(3, 0) 返回的子字符串的的长度是 3。
      

  6.   

    我是个新手,不知道理解的对不对substring()是从第几个截取到第几个,且字符串是从0开始的比如String str = "smiles";out.print(str.substring(1,2));输出:m你的代码:dataRec.substring(6,4)意思是从第六个截取到第四个就出错了
      

  7.   

    subString(),两个参数有顺序的啊,楼上的应该说得没错
      

  8.   

    第一个参数是 开始(>=0,<=length.包含此字符)
    第二个参数是 结束(>=开始,<=length.不包含此字符)
    "01234567890".substring(0,0)="";
    "01234567890".substring(0,3)="012";
    "01234567890".substring(0,10)="0123456789";//error
    //"01234567890".substring(1,0);
    //"01234567890".substring(0,1);
    //"01234567890".substring(0,11);
      

  9.   

    public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > count) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }
    return ((beginIndex == 0) && (endIndex == count)) ? this :
        new String(offset + beginIndex, endIndex - beginIndex, value);
        }
    你的一定是这个异常:throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    endIndex是不能小于beginIndex
      

  10.   

    楼上几位已经说得和明白,搂主还不结贴???
    难道还不明白。那我直接告诉你答案算了:将你str = dataRec.substring(6,4);这一句中的4改成10,即:str = dataRec.substring(6,10);如果你还不明白,我就吐血了!
      

  11.   

    将你的str = dataRec.substring(6,4)改为str = dataRec.substring(6,10);
      

  12.   

    呵呵,是用的语言太多了吧。好象SQL的存储过程里面这么截取。
      

  13.   

    MARK一下有时语言会太多容易搞乱
      

  14.   

    这里和sql不大一样..sql中得substring()索引是从1开始得
      

  15.   

    是顺序错误。
    注意API中所说:
    substring 
    public String substring(int beginIndex,
                            int endIndex)Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. 
    Examples:  "hamburger".substring(4, 8) returns "urge"
     "smiles".substring(1, 5) returns "mile"
     
    Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive. 
    Returns:
    the specified substring. 
    注意这里:抛出异常的情况!!!!说明beginIndex不能大于endIndex
    Throws: 
    IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
      

  16.   

    out.print(dataRec);
      String str;
      str = dataRec.substring(6,4);
    哈哈,上边明显错了! 要是(4, 6)还差不多,JAVA和别的语言的substring不太一样
    第一个是起始位,第二个是取到字符串的第几位
    例如:out.print(dataRec);
      String str;
      str = dataRec.substring(4, 6);
    结果是dataRec的第5 6个字符 要是还不明白就不办法了
      

  17.   

    根据个人经验。。用java最好别在字符串上做什么文章。。因为java的字符串处理能力实在是弱的不一般。如果是JSP那么最好尽可能用SCRIPT来处理相关的字符串操作。。这样你会觉得上帝一直是很爱你的。~!!!!!!
      

  18.   

    java_skater(GoRillaZ) :严重不同意!!! 不懂就不要乱说话。
      

  19.   

    out.print(dataRec);
      String str;
      str = dataRec.substring(6,4);
      
    明明dataRec是十五个字符,怎么会出错呢?
    ------------------------------------str = dataRec.substring(6,10);
      

  20.   

    我也遇到类似的事情。其实是这样的,Java和.net中substring()区别。
    .net中第二个参数指的是截取字符数个数。比如a=123456789; b=a.substring(2,4);b的结果为3456;Java中第二个参数指的是字符的结束位置,比如a=123456789; b=a.substring(2,4) ;b的结果为34;应该是这样的。各位大哥们看看。