ASP-Classic:如果String为EMPTY,则组织Query.String

时间:2013-12-10 22:42:15

标签: vbscript asp-classic

我正在向一个看起来像这样的页面发送一个字符串:

CalMiles_MS.asp?s0=Chicago,IL&s1=Akron,OH&s2=,EMPTY&s3=,EMPTY&s4=,EMPTY&s5=,EMPTY&s6=,EMPTY&s7=,EMPTY&s8=,EMPTY&s9=,EMPTY&s10=,EMPTY&s11=,EMPTY&s12=,EMPTY&s13=Des Moines,IA&s14=,EMPTY&s15=,EMPTY&s16=,EMPTY&s17=,EMPTY&s18=,EMPTY&s19=,EMPTY&s20=,EMPTY&s21=,EMPTY&s22=Miami,FL&s23=Dallas,TX

但是在"CalMiles_MS.asp"页面中我想删除所有",EMPTY"并重新排序"&s" + Number(s),所以完成上面的示例后会如下所示:

CalMiles_MS.asp?s0=Chicago,IL&s1=Akron,OH&s2=Des Moines,IA&s3=Miami,FL&s4=Dallas,TX

我该怎么做?

请帮助,谢谢。

编辑:

<%
Dim key
Dim qstr
Dim count: count = 0

Response.Write "<p>Input:<br />" & Request.Querystring & "</p>"

for each key in Request.Querystring
    If Request.Querystring(key) <> ",EMPTY" Then
      qstr = qstr & "&s" & count & "=" & Request.Querystring(key)
        count = count + 1
    End If
Next

If qstr <> "" Then
    qstr = Right(qstr, Len(qstr) - 1)
End If

Response.Write "<p>Output:<br />" & qstr & "</p>"
%>

但现在的问题是 ,它没有保持S(S#)的顺序,现在工作正常,但城市,州不在正确的顺序:

 &s1......,.......&s2......,.......&s3......,.......

我如何以数字方式对它们进行排序并使顺序保持不变(城市,州)?

我的测试查询输入:

s0=Columbus,OH&s1=,EMPTY&s2=,EMPTY&s3=Chicago,IL&s4=,EMPTY&s5=,EMPTY&s6=,EMPTY&s7=,EMPTY&s8=Akron,OH&s9=,EMPTY&s10=,EMPTY&s11=Plainfield,IN&s12=,EMPTY&s13=Miami,FL&s14=,EMPTY&s15=,EMPTY&s16=,EMPTY&s17=,EMPTY&s18=,EMPTY&s19=,EMPTY&s20=,EMPTY&s21=Memphis,TN&s22=Denver,CO&s23=Dallas,TX

输出:

s0=Columbus,OH&s1=Chicago,IL&s2=Miami,FL&s3=Plainfield,IN&s4=Dallas,TX&s5=Memphis,TN&s6=Akron,OH&s7=Denver,CO

但输出应为:

 s0=Columbus,OH&s1=Chicago,IL&s2=Akron,OH&s3=Plainfield,IN&s4=Miami,FL&s5=Memphis,TN&s6=Denver,CO&s7=Dallas,TX

2 个答案:

答案 0 :(得分:2)

为什么不循环查询字符串检查EMPTY然后再次构建字符串?

编辑: 我测试了我的代码,参数出现了1,2,3,...

<%
Dim key
Dim qstr
Dim count: count = 0

Response.Write "<p>Input:<br />" & Request.Querystring & "</p>"

for each key in Request.Querystring
    If Request.Querystring(key) <> ",EMPTY" Then
      qstr = qstr & "&s" & count & "=" & Request.Querystring(key)
        count = count + 1
    End If
Next

If qstr <> "" Then
    qstr = Right(qstr, Len(qstr) - 1)
End If

Response.Write "<p>Output:<br />" & qstr & "</p>"
%>

输入:

s0=Chicago,IL&s1=Akron,OH&s2=,EMPTY&s3=,EMPTY&s4=,EMPTY&s5=,EMPTY&s6=,EMPTY&s7=,EMPTY&s8=,EMPTY&s9=,EMPTY&s10=,EMPTY&s11=,EMPTY&s12=,EMPTY&s13=Des%20Moines,IA&s14=,EMPTY&s15=,EMPTY&s16=,EMPTY&s17=,EMPTY&s18=,EMPTY&s19=,EMPTY&s20=,EMPTY&s21=,EMPTY&s22=Miami,FL&s23=Dallas,TX

输出:

s0=Chicago,IL&s1=Akron,OH&s2=Des Moines,IA&s3=Dallas,TX&s4=Miami,FL

答案 1 :(得分:2)

您需要使用数字索引器来访问集合,而不是for each。后者保留输入字符串的顺序。

<%
Dim qstr
Dim count: count = 0
Dim x

Dim value

for x = 1 To Request.Querystring.Count
    value = Request.Querystring.Item(x)

    If value <> ",EMPTY" Then
        qstr = qstr & "&s" & count & "=" & value
        count = count + 1
    End If
Next

If qstr <> "" Then
    qstr = Right(qstr, Len(qstr) - 1)
End If

Response.Write "<p>Input:<br />" & Request.Querystring & "</p>"
Response.Write "<p>Output:<br />" & qstr & "</p>"
%>

输入:

  

S0 =哥伦布,OH&安培; S1 =,EMPTY&安培; S2 =,EMPTY&安培; S3 =芝加哥,IL&安培; S4 =,EMPTY&安培; S5 =,EMPTY&安培; S6 =,EMPTY&安培; S7 =,EMPTY&安培; S8 =俄亥俄州阿克伦&安培; S9 =,EMPTY&安培; S10 =,EMPTY&安培; S11 =普兰菲尔德,IN&安培; S12 =,EMPTY&安培; S13 =迈阿密,FL&安培; S14 =,EMPTY&安培; S15 =,EMPTY&安培; S16 =,EMPTY&安培; S17 =,EMPTY&安培; S18 = ,EMPTY&安培; S19 =,EMPTY&安培; S20 =,EMPTY&安培; S21 =孟菲斯,田纳西&安培; S22 =丹佛,CO&安培; S23 =达拉斯,德克萨斯

输出:

  

S0 =哥伦布,OH&安培; S1 =芝加哥,IL&安培; S2 =俄亥俄州阿克伦&安培; S3 =普兰菲尔德,IN&安培; S4 =迈阿密,FL&安培; S5 =孟菲斯,田纳西&安培; S6 =丹佛,CO&安培; S7 =达拉斯,德克萨斯