DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> ASP 過濾數組重復數據函數(加強版)
ASP 過濾數組重復數據函數(加強版)
編輯:關於JavaScript     
函數代碼:
復制代碼 代碼如下:
<%'*******************************************************
'過濾數組重復函數名稱:array_no(cxstr1,cxstr2,cxstr3)
'cxstr1:任意的字符串,自動識別
'cxstr2:cxstr1中分割符號。
'cxstr3:提取結果中的某一位置字串,等於0時返回為全部,大於數組下標時返回最後.
'使用於二維數組
'*******************************************************
function array_no(cxstr1,cxstr2,cxstr3)
if len(cxstr3) > 0 then
if not IsNumeric(cxstr3) then
array_no = "對不起,參數3類型必需為數字"
Exit Function
end if
else
array_no = "對不起,參數3類型必需為數字"
Exit Function
end if
if isarray(cxstr1) then
array_no = "對不起,參數1不能為數組"
Exit Function
end if
if cxstr1 = "" or isempty(cxstr1) then
array_no = "沒有數據"
Exit Function
end if
ss = split(cxstr1,cxstr2)
cxs=cxstr2&ss(0)&cxstr2
sss=cxs
for m = 0 to ubound(ss)
cc = cxstr2&ss(m)&cxstr2
if instr(sss,cc)=0 then
sss = sss&ss(m)&cxstr2
end if
next
array_no = right(sss,len(sss)-len(cxstr2))
array_no = left(array_no,len(array_no)-len(cxstr2))
if cxstr3 <> 0 then
cx_sp = split(array_no,cxstr2)
if cxstr3 > ubound(cx_sp) then
array_no = cx_sp(ubound(cx_sp))
else
array_no = cx_sp(cxstr3)
end if
end if
end function%>


下面是測試代碼:
復制代碼 代碼如下:
<%s1 = "abc,aa,bb,cdef,bc,abcdef,hhgg,gggg,cde,edc"
s2 = "1,2,3,11,22,33,12,13,14,11,33,333,14"
s3 = ""
s4 = "sdf,abc,12,2,2,abc"
s5 = split(s4)
response.write "字串為字符時:"&array_no(s1,",",0)&"<br>"
response.write "字串為數字時:"&array_no(s2,",",0)&"<br>"
response.write "字串為空時:"&array_no(s3,",",0)&"<br>"
response.write "字串為混合時:"&array_no(s4,",",0)&"<br>"
response.write "字串為數組時:"&array_no(s5,",",0)&"<br>"
response.write "字串為未知變量時:"&array_no(s33,",",0)&"<br>"
response.write "提取某一位時,沒有超過下標時:"&array_no(s1,",",2)&"<br>"
response.write "提取某一位時,超過下標時:"&array_no(s1,",",200)&"<br>"%>

測試結果:
復制代碼 代碼如下:
字串為字符時:abc,aa,bb,cdef,bc,abcdef,hhgg,gggg,cde,edc
字串為數字時:1,2,3,11,22,33,12,13,14,333
字串為空時:沒有數據
字串為混合時:sdf,abc,12,2
字串為數組時:對不起,參數1不能為數組
字串為未知變量時:沒有數據
提取某一位時,沒有超過下標時:bb
提取某一位時,超過下標時:edc


增強版本: 解決了數組常見錯誤
復制代碼 代碼如下:
<%
'*******************************************************
'過濾數組重復函數名稱:array_no(cxstr1,cxstr2,cxstr3)
'cxstr1:任意的字符串,自動識別
'cxstr2:cxstr1中分割符號。
'cxstr3:提取結果中的某一位置字串,等於0時返回為全部,大於數組下標時返回最後.
'使用於二維數組
'*******************************************************
function array_no(cxstr1,cxstr2,cxstr3)
if len(cxstr3) > 0 then
if not IsNumeric(cxstr3) then
array_no = "對不起,參數3類型必需為數字"
Exit Function
end if
else
array_no = "對不起,參數3類型必需為數字"
Exit Function
end if
if isarray(cxstr1) then
array_no = "對不起,參數1不能為數組"
Exit Function
end if
if cxstr1 = "" or isempty(cxstr1) then
array_no = "沒有數據"
Exit Function
end if
do while instr(cxstr1,",,")>0
cxstr1=replace(cxstr1,",,",",")
loop
if right(cxstr1,1)="," then
cxstr1=left(cxstr1,len(cxstr1)-1)
end if
ss = split(cxstr1,cxstr2)
cxs=cxstr2&ss(0)&cxstr2
sss=cxs
for m = 0 to ubound(ss)
cc = cxstr2&ss(m)&cxstr2
if instr(sss,cc)=0 then
sss = sss&ss(m)&cxstr2
end if
next
array_no = right(sss,len(sss)-len(cxstr2))
array_no = left(array_no,len(array_no)-len(cxstr2))
if cxstr3 <> 0 then
cx_sp = split(array_no,cxstr2)
if cxstr3 > ubound(cx_sp) then
array_no = cx_sp(ubound(cx_sp))
else
array_no = cx_sp(cxstr3)
end if
end if
end function

s1 = "abc,aa,bb,cdef,bc,abcdef,hhgg,gggg,cde,edc,333,,,,,333,7,,,,"
s2 = "1,2,3,11,22,33,12,13,14,11,33,333,14,333,,,,,333,7,,,,"
s3 = ""
s4 = "sdf,abc,12,2,2,abc,333,,,,,333,7,,,,"
s5 = split(s4)
response.write "字串為字符時:"&array_no(s1,",",0)&"<br>"
response.write "字串為數字時:"&array_no(s2,",",0)&"<br>"
response.write "字串為空時:"&array_no(s3,",",0)&"<br>"
response.write "字串為混合時:"&array_no(s4,",",0)&"<br>"
response.write "字串為數組時:"&array_no(s5,",",0)&"<br>"
response.write "字串為未知變量時:"&array_no(s33,",",0)&"<br>"
response.write "提取某一位時,沒有超過下標時:"&array_no(s1,",",2)&"<br>"
response.write "提取某一位時,超過下標時:"&array_no(s1,",",200)&"<br>"
%>

主要是增加了判斷
復制代碼 代碼如下:
do while instr(cxstr1,",,")>0
cxstr1=replace(cxstr1,",,",",")
loop
if right(cxstr1,1)="," then
cxstr1=left(cxstr1,len(cxstr1)-1)
end if
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved