Reputation: 1069
I have a string which i want to convert into a string indexed array for easy use.
Below is my expectations and outcomes.
Can someone guide me through this?
String: Dim QueryResponse = "TVShow=Adventure Time" & vbCrLf & "Color=Red"
Array:
Array
(
["TVShow"] => "Adventure Time"
["Color"] => "Red"
)
My current code:
Dim result() As String = QueryResponse.Split({vbCrLf}, StringSplitOptions.RemoveEmptyEntries)
Current code array:
Array
(
[0] => "TVShow=Adventure Time"
[1] => "Color=Red"
)
Would love some opinions on this one, thanks!
Upvotes: 0
Views: 65
Reputation: 3007
Use a dictionary instead of an array
Dim dictionary1 As New Dictionary(Of String, String)
Dim result() As String = QueryResponse.Split({vbCrLf}, StringSplitOptions.RemoveEmptyEntries)
Dim res1 As String = result(0)
Dim res2 As String = result(1)
'Split res1 and res2 into arrays using '=' as delimeter
Dim res1s() As String = Split(res1,"=")
Dim res2s() As String = Split(res2,"=")
dictionary1.Add(res1(0),res1(1))
dictionary1.Add(res2(0),res2(1))
To access the dictionary entries, use
Dim pair As KeyValuePair(Of String, String)
For Each pair In dictionary1
You can access the values here using `pair.key` and `pair.value`
'Eg Label1.Text = pair.key or Console.WriteLine(pair.value)
Next
Upvotes: 2