代码之家  ›  专栏  ›  技术社区  ›  William

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

  •  0
  • William  · 技术社区  · 7 年前

    我有一个字符串,我想转换成一个字符串索引数组,以便于使用。

    字符串: 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 回复  |  直到 7 年前
        1
  •  2
  •   preciousbetine    7 年前

    使用字典而不是数组

      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