views:

41

answers:

2

Hi there,

I want to read a string that looks exactly like this:

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

...into a Dictionary preferably. I've tried splitting on the vbcrlf (there is one on every line I can assure you) and on the '=', but can't quite get it to behave correctly often getting out of bounds errors. Anyone got some fantastic ideas on how to solve this?

Help, as always, appreciated.

+1  A: 

You could use a 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
Darin Dimitrov
Thanks Darin, I solved the problem with a bit of convoluted code (as below), but may well change to this - seems much more elegent. Thanks +1.
Chris Laythorpe
A: 

I solved this with this rather nasty piece of code;

    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

I hope it helps someone out there and if anyone can suggest improvements, please do :)

Chris Laythorpe