从字符串创建字符串索引数组

时间:2018-11-26 11:33:04

标签: arrays vb.net

我有一个字符串,我想将其转换为易于使用的字符串索引数组。

以下是我的期望和结果。

有人可以引导我完成这个过程吗?

字符串:Dim QueryResponse = "TVShow=Adventure Time" & vbCrLf & "Color=Red"

数组:

Array
(
    ["TVShow"] => "Adventure Time"
    ["Color"] =>  "Red"
)

我当前的代码: Dim result() As String = QueryResponse.Split({vbCrLf}, StringSplitOptions.RemoveEmptyEntries)

当前代码数组:

Array
(
    [0] => "TVShow=Adventure Time"
    [1] =>  "Color=Red"
)

希望对此发表一些意见,谢谢!

1 个答案:

答案 0 :(得分:2)

使用字典而不是数组

  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))

要访问词典条目,请使用

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