by Mikael Henriksson
12. November 2009 17:08
Well I sort of get it can be a pain in the ass to have to deal with strings. But what if you also have to deal with VB.NET at the same time? I just have to show this nice little helper I have.
Public Function ConvertToDictionary(ByVal input As String) As IDictionary(Of String, String)
Dim dic As New Dictionary(Of String, String)
Dim pairsplitter As Char = Char.Parse("&")
Dim keysplitter As Char = Char.Parse("=")
Dim stringArray As String() = input.Split(pairsplitter)
If (input.Length > 0) Then
If (stringArray(0).Contains("?")) Then
Dim tmp As String = stringArray(0)
stringArray(0) = tmp.Remove(0, tmp.IndexOf("?") + 1)
End If
End If
For Each keyvalpair As String In stringArray
Dim keyarray As String() = keyvalpair.Split(keysplitter)
dic.Add(keyarray(0), keyarray(1))
Next
Return dic
End Function
The fun part is when you have created your unit test and it succeeds. The boring part is revisiting such a bastard after a year trying to figure out what the hell you where doing there in the first place. Oh and to answer your question. The reason for all this is that sometimes the input stream contains the page.aspx? and sometimes it just starts with the key-value pairs and since I don’t want the page.aspx? as a key in the dictionary I remove it and replace whatever is in array(0) :) Fun times…
58475449-97b7-4594-b917-3099d15d1290|0|.0
Tags: vb.net
vb.net