我在串列框中有这样的项目
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
uj5u.com热心网友回复:
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 评论