结合Split()的结果

时间:2012-01-10 09:19:16

标签: asp-classic

我遇到以下问题:

通过MS Access DB中的链接表检索以下字符串:

string = Text1, Text2, Text3, "This, belongs, together", Text7, Text8, "This, Also, Belongs, Together", Text13, etc., etc., etc.

引用字段的长度可能会有所不同。

当我使用Split(字符串,“,”)时,它现在返回13个值,并且我可以删除引号,替换没问题。 我面临的问题是引号之间的文本应该是1值。 在上面的示例中,这意味着我应该获得8个值而不是13个。这是对数据库进行INSERT INTO查询所必需的。

到目前为止我得到的是错误: 下标超出范围:'i'

SqlJunk = "SELECT * FROM Con_Temp" 
Set rsCon = Server.CreateObject("ADODB.Recordset")
rsCon.Open SqlJunk, dbGlobalWeb, 3

Do While Not rsCon.EOF
Field = split(rsCon("Field1"),",")
For i = 0 to UBound(Field)
    If InStr(Field(i),"""") > 0 Then
        Field(i) = Replace(Field(i), """", "")
    End if
    If Field(i) <> "" Then
        If dbfields <> "" Then
            dbfields = dbfields & ",[" & Conveldnaam(i) & "]"
        Else
            dbfields = "[" & Conveldnaam(i) & "]"
        End if
        If dbvalues <> "" Then
            dbvalues = dbvalues & ",""" & Field(i) & """"
        Else
            dbvalues = """" & Field(i) & """"
        End if
    End if
    response.write(dbfields)
Next
SQL = "INSERT INTO ConInventory (" & dbfields & ") VALUES (" & dbvalues & ")"
response.write(SQL & "<br>")
dbGlobalWeb.Execute(SQL)
rsCon.MoveNext
dbfields = ""
dbvalues = ""
Loop

我是否可以将引号中的值组合起来?

提前致谢!

埃里克

1 个答案:

答案 0 :(得分:0)

尝试此逻辑并相应地修改代码: -

Dim str, strArray, count, combineStr
count = 0
combineStr = ""
str = "Text1, Text2, Text3, ""This, belongs, together"", Text7, Text8, ""This, Also, Belongs, Together"", Text13"
strArray = split(str, ", ")


For Each item In strArray
    If (Left(item, 1) = """") Then
        combineStr = combineStr & " " & item
        count = 1
    ElseIf (count = 1) Then
        If (Right(item, 1) = """") Then
            count = 0
        End If
        combineStr = combineStr & " " & item
    End If
    If (count = 0) Then
        If (combineStr = "") Then
            Response.Write("<br>" & item)
        ElseIf (combineStr <> "") Then
            Response.Write("<br>" & combineStr)
            combineStr = ""
        End If
    End If 
Next