为什么我的string长度明明够,但是substring总是出错啊?有高手指点一下吗?
解决方案 »
- 图片为啥显示不出来?
- 通过按钮获得div 的id
- 求一个cxf+spring的例子,要能通
- 内置对象与隐式对象有哪些区别啊
- 有关排序的如何实现的问题,请教高手!!!!!!!!!!!!!
- 一道数据库面试题
- 来讨论吧!!!关于servlet的生成期问题,烦啊!!!!请看以下程序!!!!!!!!!!!!!!!!!!!!!!!
- 紧急求救!!如何用让HTML转成的WORD输出里, 设置它的表头,就是让这个表头在每一页都显示
- 请问在表单上把一个select设置为readonly然后在request中就取不到值了。
- servlet 的init()方法都有什么用?没有东西要初始化的时候也要写上吗?
- 关于JFreeChart图片显示不了,在线请教
- JSP中如何捕获到404错误?
str=str.subString(ints,inte);
我用过,不会有问题。
String str;
str = dataRec.substring(6,4);
明明dataRec是十五个字符,怎么会出错呢?
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.
注意:第二个参数并不是截取的子串的长度,而是要截取的终止位置.
String str;
str = dataRec.substring(6,4);
如果按你说的是没错。
我想问题还是在dataRec里
你能保证有15位吗?
如果是真的那就是JDK的问题。
"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。
第二个参数是 结束(>=开始,<=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);
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
难道还不明白。那我直接告诉你答案算了:将你str = dataRec.substring(6,4);这一句中的4改成10,即:str = dataRec.substring(6,10);如果你还不明白,我就吐血了!
注意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.
String str;
str = dataRec.substring(6,4);
哈哈,上边明显错了! 要是(4, 6)还差不多,JAVA和别的语言的substring不太一样
第一个是起始位,第二个是取到字符串的第几位
例如:out.print(dataRec);
String str;
str = dataRec.substring(4, 6);
结果是dataRec的第5 6个字符 要是还不明白就不办法了
String str;
str = dataRec.substring(6,4);
明明dataRec是十五个字符,怎么会出错呢?
------------------------------------str = dataRec.substring(6,10);
.net中第二个参数指的是截取字符数个数。比如a=123456789; b=a.substring(2,4);b的结果为3456;Java中第二个参数指的是字符的结束位置,比如a=123456789; b=a.substring(2,4) ;b的结果为34;应该是这样的。各位大哥们看看。