dim函數(shù)第三個(gè)參數(shù)設(shè)置截取字符的長度問題
更新時(shí)間:2012年12月10日 17:34:24 作者:
dim函數(shù)的第三個(gè)參數(shù),也就是截取字符的長度,我在設(shè)置這個(gè)的時(shí)候,出了些問題,提示無效的過程調(diào)用或參數(shù),本文詳細(xì)介紹如何解決
dim函數(shù)的第三個(gè)參數(shù),也就是截取字符的長度,我在設(shè)置這個(gè)的時(shí)候,出了些問題:response.write mid(up_address,a(i),a(i+1)-1) & “<br />”像我上面這樣寫的時(shí)候,它就會(huì)報(bào)錯(cuò),提示無效的過程調(diào)用或參數(shù),
response.write mid(up_address,a(i),a(i+1)+1) & “<br />”但是當(dāng)我把其中的a(i+1)-1改為a(i+1)+1時(shí),就能執(zhí)行了,a(i+1)對(duì)應(yīng)的值是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的第三個(gè)參數(shù)那出了點(diǎn)問題,我本來是想這樣截取的:
VB code:
mid(up_address,a(i),a(i+1)-a(i)-1)
‘a(chǎn)(i)是空格的位置
‘a(chǎn)(i+1)是下一個(gè)空格的位置
‘a(chǎn)(i+1)-a(i)-1是兩個(gè)空格直間的字符長度
現(xiàn)在的問題是,經(jīng)測(cè)試,mid的第三個(gè)參數(shù)那,無法使用減法,也就是說,我可以寫a(i+1)+,但不能寫a(i+1)-,想了好久,我一直不明白問題出在哪?應(yīng)該怎么來解決呢?
出現(xiàn)這個(gè)問題是因?yàn)樯厦娴腗ID函數(shù)的第三個(gè)參數(shù)出現(xiàn)了負(fù)數(shù),下面是在網(wǎng)上找的測(cè)試的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)‘這里的結(jié)果為34,-1,所以導(dǎo)致出錯(cuò)
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 />”但是當(dāng)我把其中的a(i+1)-1改為a(i+1)+1時(shí),就能執(zhí)行了,a(i+1)對(duì)應(yīng)的值是11,可為什么只能減不能加呢?全部代碼如下:
VB code:
復(fù)制代碼 代碼如下:
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的第三個(gè)參數(shù)那出了點(diǎn)問題,我本來是想這樣截取的:
VB code:
復(fù)制代碼 代碼如下:
mid(up_address,a(i),a(i+1)-a(i)-1)
‘a(chǎn)(i)是空格的位置
‘a(chǎn)(i+1)是下一個(gè)空格的位置
‘a(chǎn)(i+1)-a(i)-1是兩個(gè)空格直間的字符長度
現(xiàn)在的問題是,經(jīng)測(cè)試,mid的第三個(gè)參數(shù)那,無法使用減法,也就是說,我可以寫a(i+1)+,但不能寫a(i+1)-,想了好久,我一直不明白問題出在哪?應(yīng)該怎么來解決呢?
出現(xiàn)這個(gè)問題是因?yàn)樯厦娴腗ID函數(shù)的第三個(gè)參數(shù)出現(xiàn)了負(fù)數(shù),下面是在網(wǎng)上找的測(cè)試的VBS代碼,原理一樣,如下的代碼:
VBScript code:
復(fù)制代碼 代碼如下:
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)‘這里的結(jié)果為34,-1,所以導(dǎo)致出錯(cuò)
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
相關(guān)文章
VBScript教程 第二課在HTML頁面中添加VBscript代碼
VBScript教程 第二課在HTML頁面中添加VBscript代碼...2006-11-11
VBS教程:VBscript屬性-HelpContext 屬性
VBS教程:VBscript屬性-HelpContext 屬性...2006-11-11
VBS實(shí)現(xiàn)工作表按指定表頭自動(dòng)分表
下面的VBS腳本就是實(shí)現(xiàn)的工作表按指定表頭(由用戶選擇)自動(dòng)分表功能。需要的朋友只要將要操作的工作表拖放到腳本文件上即可輕松實(shí)現(xiàn)工作表分表2013-01-01
VBS教程:函數(shù)-DateDiff 函數(shù)
VBS教程:函數(shù)-DateDiff 函數(shù)...2006-11-11
vbs 腳本錯(cuò)誤:操作中文件名或類名未找到的解決方法
昨天下午閑得無事,于是翻出以前的腳本,嘗試運(yùn)行,結(jié)果卻出現(xiàn)了如圖所示的錯(cuò)誤2013-07-07
vbs循環(huán)產(chǎn)生的參數(shù)的傳遞問題
用vbs實(shí)現(xiàn)循環(huán)產(chǎn)生參數(shù)的傳遞問題,建議大家測(cè)試學(xué)習(xí)2008-06-06

