0

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!

2
  • 4
    Your expected result is more a Dictionary<string,string> than an array Commented Nov 26, 2018 at 11:37
  • Perfect! I have implemented that, now just researching some small things and im away :D Thanks! Commented Nov 26, 2018 at 12:12

1 Answer 1

2

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      
Sign up to request clarification or add additional context in comments.

1 Comment

Was writing up my own answer which is technically similar so i will accept this, saves me writing the rest up :D Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.