asp下去除数组中重复项的方法
程序员文章站
2023-08-24 11:55:47
复制代码 代码如下:<%function mover(rstr) dim i,spstr spstr = split(rstr,",") for i = 0 to u...
复制代码 代码如下:
<%
function mover(rstr)
dim i,spstr
spstr = split(rstr,",")
for i = 0 to ubound(spstr)
if i = 0 then
mover = mover & spstr(i) & ","
else
if instr(mover,spstr(i))=0 and i=ubound(spstr) then
mover = mover & spstr(i)
elseif instr(mover,spstr(i))=0 then
mover = mover & spstr(i) & ","
end if
end if
next
end function
response.write mover("abc,abc,dge,gcg,dge,gcg,die,dir,die")%>
结果是:abc,dge,gcg,die,dir
如果是两个数组进行对比,去掉相同的数组元素:
复制代码 代码如下:
<%
function mover(farray,sarray)
a = split(farray, ",")
set dic = createobject("scripting.dictionary")
for k=0 to ubound(a)
if a(k)<> "" then dic.add "_" & a(k), a(k)
next
a = split(sarray, ",")
for k=0 to ubound(a)
if a(k)<> "" then
if dic.exists("_" & a(k)) then
dic.remove "_" & a(k)
end if
end if
next
items = dic.items()
set dic = nothing
mover=join(items, ",")
end function
n1 = "a,b,1,11,12,13,14,15,16,17,19,20,22"
n2 = "a,1,12,14,18,19,20"
response.write mover(n1,n2)
%>
结果是:
3.数组a中有为空的元素(如a=array("ww","ss","","dd","","ee")),
想把这些空元素从数组a中去掉.并把去掉空元素有的数组赋给数组b.
复制代码 代码如下:
str=""
for i = lbound(a) to ubound(a)
if a(i)<>"" then
if i<>lbound(a) then str = str + "," end if
str = str & a(i)
end if
next
b = split(str,",")