【发布时间】:2014-05-27 02:49:31
【问题描述】:
这是我的字符串:
gamename\jbnightfire\hostname\testserver\hostport\26015
我正在尝试将此字符串拆分为字典(字符串,字符串)。请注意,这个字符串不是静态的,可以包含更多的键和值。
字典将包含以下内容:
key = gamename, value = jbnightfire
key = hostname, value = testserver
key = hostport, value = 26015
我已经尝试了至少 5 种不同的方法,但似乎都无法奏效。它应该非常简单,但是对于我的生活,我无法让它发挥作用。任何帮助将不胜感激。谢谢。
【问题讨论】:
-
您可以在这里发布您尝试过的至少一种方法吗?
-
每次尝试后我都删除了代码
-
我终于想出了一个可行的解决方案,它可能不是最好的解决方案,但它确实有效。 Dim stringtosplit as string = "gamename\jbnightfire\hostname\testserver\hostport\26015" Dim dictionary1 as New Dictionary(Of String, String) Dim spl = stringtosplit.Split("\") Dim temp As String = "" Dim temp2 As String = "" For i As Integer = 0 To spl1.Length - 1 If temp.Length = 0 Then temp = spl(i) Else temp2 = spl(i) dictionary1.Add(temp, temp2) temp = "" End If下一个
-
请不要在评论中发布代码。编辑您的问题并在那里发布代码。
-
我想得越多,我认为以下格式是最安全的键值:游戏名、游戏名\jbnightfire、游戏名\jbnightfire\主机名等,这样键将是唯一的
标签: vb.net string dictionary split