dim函數(shù)的第三個參數(shù),也就是截取字符的長度,我在設置這個的時候,出了些問題,提示無效的過程調用或參數(shù),本文詳細介紹如何解決
dim函數(shù)的第三個參數(shù),也就是截取字符的長度,我在設置這個的時候,出了些問題:response.write mid(up_address,a(i),a(i+1)-1) & “<br />”像我上面這樣寫的時候,它就會報錯,提示無效的過程調用或參數(shù),
response.write mid(up_address,a(i),a(i+1)+1) & “<br />”但是當我把其中的a(i+1)-1改為a(i+1)+1時,就能執(zhí)行了,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的第三個參數(shù)那出了點問題,我本來是想這樣截取的:
VB code:
代碼如下:
mid(up_address,a(i),a(i+1)-a(i)-1)
‘a(i)是空格的位置
‘a(i+1)是下一個空格的位置
‘a(i+1)-a(i)-1是兩個空格直間的字符長度
現(xiàn)在的問題是,經測試,mid的第三個參數(shù)那,無法使用減法,也就是說,我可以寫a(i+1)+,但不能寫a(i+1)-,想了好久,我一直不明白問題出在哪?應該怎么來解決呢?
出現(xiàn)這個問題是因為上面的MID函數(shù)的第三個參數(shù)出現(xiàn)了負數(shù),下面是在網上找的測試的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