如何获取重复元素的索引及其后面的空元素的范围。 LINQ

问题描述 投票:0回答:1
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"})})}

当我们在Excel中复制垂直合并的单元格时,就得到了这样一个数组。(ComplexArray) enter image description here

vb.net linq
1个回答
0
投票

如果结果类型是

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
© www.soinside.com 2019 - 2024. All rights reserved.