Dim ComplexArray As String() = Split("A C A B C A B A B C")
Dim Need_result= {({"A", ({"0-2", "5-9", "16-16", "19-20"})}), ({"B", ({"10-12", "17-18", "21-24"})}), ({"C", ({"3-4", "13-15", "25-25"})})}
如果结果类型是
Dictionary(Of String, List(Of Int32))
,您可以使用此循环:
Dim needResult As New Dictionary(Of String, List(Of Int32))
For index As Integer = 0 To complexArray.Length - 1
Dim key As String = complexArray(index)
If key = String.Empty Then
' get last key
key = complexArray.
Take(index).
Reverse().
FirstOrDefault(Function(k) k <> String.Empty)
If key IsNot Nothing Then
needResult(key).Add(index)
End If
Continue For
End If
If Not needResult.ContainsKey(key) Then
needResult.Add(key, New List(Of Int32))
End If
needResult(key).Add(index)
Next