在Classic ASP中:转换逗号分隔的字符串

时间:2015-10-23 13:38:11

标签: string asp-classic

如何在Classic ASP中转换此(示例)逗号分隔的Querystring

Illinois, Iowa, Wisconsin, Indiana, Kentucky,Missouri to

'Illinois', 'Iowa', 'Wisconsin', 'Indiana', 'Kentuck','Missouri'

我正在使用Dreamweaver并且需要这种格式来从表中选择包含字符串Using IN子句中的一种状态的记录。

2 个答案:

答案 0 :(得分:2)

如果你只需要转换,那就是:

origstring = "Illinois, Iowa, Wisconsin, Indiana, Kentucky, Missouri"
convertedstring = "'" &replace(origstring, ", ","', '") &"'"

但是,让我们说你(或其他任何人)有一个状态的查询字符串:

strinclause = ""
arrstates = Split(Request.QueryString("states"), ",")
if(Ubound(arrstates) > 0) Then 
    for i=0 to Ubound(arrstates)
      'VALIDATE/CLEAN YOUR ARRAY ITEMS AND BUILD YOUR STRING IN THIS WAY:
       strinclause = strinclause & "'" &arrstates(i)& "',"
    next
    'NOW REMOVE LAST COMA
    strinclause = Left(strinclause,Len(strinclause)-1)
end if
'HERE YOU CAN USE YOUR STRING THAT WILL BE IN THE FORMAT YOU ASKED

答案 1 :(得分:0)

Jut spitballing,但你可以这样做:

<%
    Dim qs : qs = Trim("" & Request.QueryString("states") ' e.g.  "Illinois, Iowa, Wisconsin, Indiana, Kentucky, Missouri"

    Dim cmd : Set cmd = Server.CreateObject("ADODB.Command")
    cmd.ActiveConnection = db.connection ' defined elsewhere
    cmd.ActiveConnection.CursorLocation = adUseClient ' 3
    cmd.CommandType = adCmdText ' 1
    cmd.CommandTimeout = 90

    dim inp : inp = Split(qs, ",") ' states
    dim sql, i : ReDim sql (ubound(inp)) ' for sql

    for i = 0 to ubound(inp)
            sql(i) = "select ?" ' parameter in string
            cmd.Parameters.Append cmd.CreateParameter("@p" & i, adVarChar, adParamInput, 100, inp(i)) ' actual typed parameter object
    next
    cmd.CommandText = "select id, stock from warehouse where state in (" & Join(sql, " union ") & ")"

    Set rs = cmd.Execute
    Do While Not rs.eof
            response.write "<p>" & rs("id") & ": " & rs("stock") & "</p>"
            rs.MoveNext
    Loop
    Set rs = Nothing
    Set cmd = Nothing
%>