【问题标题】:Creating string index array from string从字符串创建字符串索引数组
【发布时间】:2018-11-26 11:33:04
【问题描述】:

我有一个字符串,我想将其转换为字符串索引数组以方便使用。

以下是我的期望和结果。

有人可以指导我完成这个吗?

字符串: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"
)

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

【问题讨论】:

  • 您的预期结果更像是一个 Dictionary 而不是一个数组
  • 完美!我已经实现了,现在只是研究一些小东西,我走了:D 谢谢!

标签: arrays vb.net


【解决方案1】:

使用字典而不是数组

  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      

【讨论】:

  • 正在写我自己的答案,这在技术上是相似的,所以我会接受这个,省去我写剩下的时间:D 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 2019-11-24
相关资源
最近更新 更多