dim函数第三个参数设置截取字符的长度问题

时间:2021-05-22

dim函数的第三个参数,也就是截取字符的长度,我在设置这个的时候,出了些问题:response.write mid(up_address,a(i),a(i+1)-1) & “<br />”像我上面这样写的时候,它就会报错,提示无效的过程调用或参数,
response.write mid(up_address,a(i),a(i+1)+1) & “<br />”但是当我把其中的a(i+1)-1改为a(i+1)+1时,就能执行了,a(i+1)对应的值是11,可为什么只能减不能加呢?全部代码如下:
VB code:
复制代码 代码如下:
dim a(),up_address
up_address = "aaa djaldk adflj adafadfasdfa afd ad"
redim a(len(up_address))
a(0) = instr(up_address," ")
response.write a(0) & "<br />"
if a(0)<>0 then
for i=0 to len(up_address)-1
a(i+1) = instr(a(i)+1,up_address," ")
response.write mid(up_address,a(i),a(i+1)-1) & "<br />"
if a(i+1)=0 then
exit for
end if
response.write a(i+1) & "<br />"
next

end if如上代码,我是想把字符串按空格分解出来,但是mid的第三个参数那出了点问题,我本来是想这样截取的:
VB code:
复制代码 代码如下:
mid(up_address,a(i),a(i+1)-a(i)-1)
‘a(i)是空格的位置
‘a(i+1)是下一个空格的位置
‘a(i+1)-a(i)-1是两个空格直间的字符长度

现在的问题是,经测试,mid的第三个参数那,无法使用减法,也就是说,我可以写a(i+1)+,但不能写a(i+1)-,想了好久,我一直不明白问题出在哪?应该怎么来解决呢?
出现这个问题是因为上面的MID函数的第三个参数出现了负数,下面是在网上找的测试的VBS代码,原理一样,如下的代码
VBScript code:
复制代码 代码如下:
dim a(),up_address
up_address = "aaa djaldk adflj adafadfasdfa afd ad"
MsgBox len(up_address) '36
redim a(len(up_address)) 'a(36)
a(0) = instr(up_address," ")
MsgBox a(0) 'a(0)=4
MsgBox a(0) & "<br />"
if a(0)<>0 then
for i=0 to len(up_address)-1
a(i+1) = instr(a(i)+1,up_address," ")
MsgBox a(i) &" "& (a(i+1)-1)‘这里的结果为34,-1,所以导致出错
MsgBox mid(up_address,a(i),a(i+1)-1) & "<br />"
if a(i+1)=0 then
exit for
end if
MsgBox a(i+1) & "<br />"
next
end if

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章