dim函数第三个参数设置截取字符的长度问题
程序员文章站
2022-07-04 20:35:12
dim函数的第三个参数,也就是截取字符的长度,我在设置这个的时候,出了些问题:response.write mid(up_address,a(i),a(i+1)-1) &...
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
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
下一篇: 姜维为何会选择投降蜀汉?真相是什么