我在串列框中有這樣的專案
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
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/409118.html
標籤:
