如何通过多分割过滤器获取字符串位置和长度

问题描述 投票:0回答:1

enter image description here

我正在尝试创建我期望的子例程(GetAllPosition)。 我不知道在代码中哪里使用

RsultOfPostionAndLength
才能获得预期的输出

我对(

RsultOfPostionAndLength 
)输出的期望:

{"C", "B", "A"}

MultiSplit 是:
55-5 '}666['  61-5']777('    67-3')8['  71-6']hhhy('

{"A", "B", "C"}

MultiSplit 是:
27-6 ']1111{'  34-4'}22('    39-7')33333{'  47-3'}4{' 51-3 '}5{' 55-5'}666[' 78-4')99{'  83-5'}999['

    Dim RsultOfPostionAndLength As List(Of String) 'Pattern is Position of String And Length Of Find
    '  'MultiSplit' For {"C", "B", "A"} is:  55-5 '}666['  61-5']777('    67-3')8['  71-6']hhhy('
    '  'MultiSplit' For {"A", "B", "C"} is:  27-6 ']1111{'  34-4'}22('    39-7')33333{'  47-3'}4{' 51-3 '}5{' 55-5'}666[' 78-4')99{'  83-5'}999['
   

 Sub Setup()
        Dim inString As String = "}666[]777([}666[]777(]00[A]1111{C}22(B)33333{C}4{C}5{C}666[A]777(B)8[A]hhhy(B)99{C}999[A]101010}666[]777("
        Dim MultiSplit As String() = {"C", "B", "A"}
        GetAllPosition(1, inString.Length, inString, MultiSplit, 0)
    End Sub

 Sub GetAllPosition(LastIdx As Integer, LastLen As Integer, inString As String, MultiSplit As String(), Level As Integer)
        Dim spl As String() = Split(inString, ",")
        Dim body As String = Mid(inString, LastIdx, LastLen)
        'Dim LastIdx As Integer = spl(0)
        Dim results As New List(Of String)
        Dim Find As String = MultiSplit(Level)
        Dim last As Integer = LastIdx
        Dim index As Integer = -1
        Do Until body.Length <= index
            index = body.IndexOf(Find, index + 1)
            If index = -1 AndAlso body.Length > last Then
                index = body.Length
            End If
            Dim lenTexxt As Integer = Math.Abs(last - index) + 1
            Dim part1 As String = Mid(body, last, lenTexxt)
            Dim part As String = Mid(inString, last, lenTexxt)
            Dim b As Boolean = MultiSplit.Skip(Level + 1).All(Function(c) part.IndexOf(c, comparisonType:=StringComparison.OrdinalIgnoreCase) > -1)
            If b Then
                results.Add(last & "," & lenTexxt & "," & part)
            End If
            last = index + 2
        Loop
        Level += 1
        For Each g In results
            Dim spl2 As String() = Split(g, ",")
            GetAllPosition(spl2(0), spl2(1), inString, MultiSplit, Level)
        Next
    End Sub
string vb.net split indexof instr
1个回答
0
投票

您正在尝试递归方法,在这种情况下这是一件好事。不过我觉得有点过于复杂了。

我们必须用与当前级别对应的分隔符(“A”、“B”或“C”)进行分割。最后一级必须以不同的方式处理才能结束递归,而且因为我们不必测试是否包含下一级分隔符。

Sub Test()
    Dim inString As String = "}666[]777([}666[]777(]00[A]1111{C}22(B)33333{C}4{C}5{C}666[A]777(B)8[A]hhhy(B)99{C}999[A]101010}666[]777("
    Dim MultiSplit As String() = {"A", "B", "C"}

    Dim result As List(Of String) = AllPositions(inString, MultiSplit, 0)

    For Each s As String In result
        Console.WriteLine(s)
    Next
End Sub

Public Function AllPositions(input As String, separators As String(), level As Integer) As List(Of String)
    Dim separator As String = separators(level)
    Dim ignoreCaseSeparators As String() = New String() {separator.ToUpperInvariant, separator.ToLowerInvariant}
    Dim parts As String() = input.Split(ignoreCaseSeparators, StringSplitOptions.None)

    If level = separators.Length - 1 Then
        Return parts.ToList()
    Else
        Dim result = New List(Of String)()
        For Each part As String In parts
            Dim nextSeparator As String = separators(level + 1)
            If part.Contains(nextSeparator, StringComparison.OrdinalIgnoreCase) Then
                result.AddRange(AllPositions(part, separators, level + 1))
            End If
        Next
        Return result
    End If
End Function
© www.soinside.com 2019 - 2024. All rights reserved.