ASP split后得到数组个数问题

比如说我接收过来的字符串按逗号隔开,正常来说会有8个元素,但有时候发送过来的元素不够,只有7个或者6个,我要如何判断它不存在,然后给不存在的数组元素赋值.
以下是我的代码,测试过多种方法,都下标越界.
<%
str="金雅,他他中,4月19日,thikpad,7400元 ,崔"
dim strbd(9)
strbd=split(str,",")
for i =0 to 8
if strbd(i) = NULL then
strbd(i)="空"
end if
next
for j = 0 to 8
response.write strbd(j)&"<br>"
next
%>

UBound什么的也都用过了的

你可以先判断数组长度,如果长度不够你指定位数则重新定义一个数组的长度
<%
str="金雅,他他中,4月19日,thikpad,7400元 ,崔"
strbd=split(str,",")
'判断数组是否是8个元素
if Ubound(strbd)<7 then
ReDim Preserve strbd(7) '重新定义数组长度
end if

for i =0 to UBound(strbd)
if strbd(i) = NULL or strbd(i)="" then
strbd(i)="空"
end if
next
for j = 0 to UBound(strbd)
response.write strbd(j)&"<br>"
next
%>
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-04-20
用SPLIT获取的数据是没有空值的,他会根据你的数组元素个数来定义维数,一般这种变长的数组用UBOUND就可以了
str="金雅,他他中,4月19日,thikpad,7400元 ,崔"
strbd=split(str,",")
for i=0 to ubound(strbd)
response.write strbd(i)&"<br />"
next

这样就可以直接输出整个数组的内容了,不用再去管什么空值,再去付值 ,如果必须为8个值 的话,有两种办法,你可以在字符串中加逗号来使拆分的数组增加维数,或者重定义数组维数
str="金雅,他他中,4月19日,thikpad,7400元 ,崔"
strbd=split(str,",")
if ubound(strbd)<8 then
n=ubound(strbd)
Redim preserve strbd(8)
for i=n+1 to ubound(strbd)
strbd(i)="空"
next
end if
for i=0 to ubound(strbd)
response.write strbd(i)&"<br />"
next
第2个回答  2011-04-20
<%
str="金雅,他他中,4月19日,thikpad,7400元 ,崔"
dim strbd
strbd=split(str,",")
for i =0 to UBound(strbd)
if strbd(i) = NULL then
strbd(i)="空"
end if
next
for j = 0 to UBound(strbd)
response.write strbd(j)&"<br>"
next
%>追问

这样只能取到发过来的元素个数,要是只有6个,那我后面的就没有了,我要怎么样给strbd(6),(7),(8)赋值

追答

这个,你不嫌麻烦的话,可以用另外的数组把这个数组装过来..虽然我觉得没什么实际意义..这个数组如果装的是你传进来的对象,你就没必要往这个对象里添加些没必要的东西...不过我不知道有没有类似的数据追加函数..因为我没遇到过这种情况

第3个回答  2011-04-20
str="金雅,他他中,4月19日,thikpad,7400元 ,崔,,"
strbd=split(str,",")
for i =0 to Ubound(strbd)
if strbd(i) = "" or isNULL(strbd(i)) then
strbd(i)="空"
end if
next
for j = 0 to Ubound(strbd)
response.write strbd(j)&"<br>"
next
第4个回答  2011-04-20
<%
str="金雅,他他中,4月19日,thikpad,7400元,崔"
dim strbd(9),strbd2
strbd2 = split(str,",")
for i = 0 to 8
if(i <= ubound(strbd2)) then
strbd(i) = strbd2(i)
else
strbd(i) = "空"
end if
next

for j = 0 to 8
response.write strbd(j)&"<br>"
next
%>
相似回答