dim函数第三个参数设置截取字符的长度问题
dim函数第三个参数设置截取字符的长度问题
发布时间:2016-12-28 来源:查字典编辑
摘要:dim函数的第三个参数,也就是截取字符的长度,我在设置这个的时候,出了些问题:response.writemid(up_address,a(...

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

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新vbs学习
热门vbs学习
脚本专栏子分类