ASP 過濾數組重複資料函數(加強版)

來源:互聯網
上載者:User

函數代碼: 複製代碼 代碼如下:<%'*******************************************************
'過濾數組重複函數名稱: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.