将相似的第一个列表框编号添加到另一个列表框 vb.net
add similar first number of listbox to another listbox vb.net
我在列表框中有项目
像这样
0,11,41,50
1,5,66,75
1,10,40,50
2,3,43,50
2,7,63,75
2,11,46,50
我需要为 1 项添加相似的起始编号
像这样
0,11,41,50
1,5,66,75 * 1,10,40,50
2,3,43,50 * 2,7,63,75 * 2,11,46,50
String.Split()
你设置“,”并使用第一项作为 KEY。使用该键将每个集合添加到 Dictionary(Of String, List(Of String))
。然后遍历 Dictionary 并使用 String.Join()
:
组合集合
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim buckets As New Dictionary(Of String, List(Of String))
For Each items In ListBox1.Items
Dim key As String = items.split(",")(0)
If Not buckets.ContainsKey(key) Then
buckets.Add(key, New List(Of String))
End If
buckets(key).Add(items)
Next
ListBox2.Items.Clear()
For Each kvp As KeyValuePair(Of String, List(Of String)) In buckets
ListBox2.Items.Add(String.Join(" * ", kvp.Value))
Next
End Sub
输出:
我在列表框中有项目 像这样
0,11,41,50
1,5,66,75
1,10,40,50
2,3,43,50
2,7,63,75
2,11,46,50
我需要为 1 项添加相似的起始编号 像这样
0,11,41,50
1,5,66,75 * 1,10,40,50
2,3,43,50 * 2,7,63,75 * 2,11,46,50
String.Split()
你设置“,”并使用第一项作为 KEY。使用该键将每个集合添加到 Dictionary(Of String, List(Of String))
。然后遍历 Dictionary 并使用 String.Join()
:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim buckets As New Dictionary(Of String, List(Of String))
For Each items In ListBox1.Items
Dim key As String = items.split(",")(0)
If Not buckets.ContainsKey(key) Then
buckets.Add(key, New List(Of String))
End If
buckets(key).Add(items)
Next
ListBox2.Items.Clear()
For Each kvp As KeyValuePair(Of String, List(Of String)) In buckets
ListBox2.Items.Add(String.Join(" * ", kvp.Value))
Next
End Sub
输出: