在Classic ASP中包含for ...每个循环的空白元素

时间:2017-07-03 22:59:19

标签: url vbscript asp-classic query-string

我尝试将VBScript数组转换为查询字符串,以包含在URL中作为Get。我有:

function array_to_querystring( arrParams )
    dim x : x=1
    for each param in arrParams
        array_to_querystring = "&" & x & "=" & param
        x=x+1
    next
end function

问题是有时候一个元素是一个空字符串,我想要包含它,但我的代码会删除它们。

所以,如果我致电array_to_querystring( array( "", "hello" ) ),我会得到:

&2=hello

我想要的是:

&1=&2=hello

for ...每个循环正在删除空字符串的参数。知道怎么强迫它包括它们吗?

1 个答案:

答案 0 :(得分:4)

这不是因为空字符串,而是因为您在每次迭代中将值分配给函数名称。

你应该在每次迭代中继续添加一个字符串,然后在循环后将字符串赋值给函数名。

function array_to_querystring( arrParams )
    dim x : x=1
    dim output : output=""
    for each param in arrParams
        output = output & "&" & x & "=" & param
        x=x+1
    next

    array_to_querystring = output
end function