0

我想读取一个看起来完全像这样的字符串:

VPSProtocol=2.22
Status=OK
StatusDetail=0000 : The Authorisation was Successful.
VPSTxId={BBF09A43-913E-14E3-B41B-E5464B6FF8A9}
SecurityKey=EH8VFZUSH9
TxAuthNo=4979698
AVSCV2=SECURITY CODE MATCH ONLY
AddressResult=NOTMATCHED
PostCodeResult=NOTMATCHED
CV2Result=MATCHED
CAVV=AAABARR5kwAAAAAAAAAAAAAAAAA=
3DSecureStatus=OK

...最好进入字典。我已经尝试在 vbcrlf (我可以向你保证的每一行都有一个)和“=”上进行拆分,但不能让它正常运行,经常出现越界错误。有人对如何解决这个问题有一些奇妙的想法吗?

一如既往,感谢您的帮助。

4

2 回答 2

1

您可以使用StringReader

Dim dic = New Dictionary(Of String, String)
Using reader = New StringReader(someString)
    Dim line As String = reader.ReadLine()
    While line <> Nothing
        Dim tokens = line.Split("=")
        If tokens.Length < 2 Then
            Continue While
        End If
        dic.Add(tokens(0), tokens(1))
        line = reader.ReadLine()
    End While
End Using
于 2010-01-26T16:18:30.247 回答
0

我用这段相当讨厌的代码解决了这个问题;

    Dim dictionary As New Dictionary(Of String, String)
    Dim x As String = "x?" & Replace(tmp.ToString(), vbCrLf, "&")
    Dim arr As String() = x.Split("?".ToCharArray())
    If arr.Length >= 2 Then
        Dim arr2 As String() = arr(1).Split("&".ToCharArray())
        For Each item As String In arr2
            Dim arr3 As String() = item.Split("=".ToCharArray())
            dictionary.Add(arr3(0), arr3(1))
        Next
    End If

我希望它可以帮助那里的人,如果有人可以提出改进建议,请做:)

于 2010-01-26T16:20:45.910 回答