How to multi-split the text while going deeper into details by level

Mansour_Dalir 1,976 Reputation points
2024-11-02T05:47:54.5333333+00:00

vb

How to multi-split the text while going deeper into details by level

'This is the input text
   Dim MyString As String = "00[A]1111{C}22(B)33333{C}4{C}5{C}666[A]777(B)8(B)99{C}999[A]101010"
'The expected output is an array as shown below
Dim Result as String()={"]1111{","}22(",")33333{","}4{","}5{","}666[",")99{","}999["}

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim MyString As String = "00[A]1111{C}22(B)33333{C}4{C}5{C}666[A]777(B)8(B)99{C}999[A]101010"
         Dim LevelSplit As String() = {"A", "B", "C"}
        MultiFinderString(MyString, LevelSplit(0), LevelSplit)
 End Sub
Dim LisCheck As New List(Of String)
    Private Function MultiFinderString(BodyText As String, find As String, Optional MultiFind As String() = Nothing) As String
        Dim result As New List(Of Integer)()
        Dim position As Integer = InStr(BodyText, find)
        If position > 0 Then
            If position > 1 Then
                result.Add(1)
            End If
            While position > 0
                result.Add(position)
                position = InStr(position + 1, BodyText, find)
            End While
            If result(result.Count - 1) < BodyText.Length Then
                result.Add(BodyText.Length)
            End If
            Dim MyInteger As Integer() = result.ToArray

Dim ResultStr As String() = MyInteger.Select(Function(x, index)
   If index < MyInteger.Length - 2 Then                                                           Return $"{x }-{MyInteger(index + 1) - 1}"                                                           Else
Return $"{x}-{MyInteger.Last() + 1}"                                                             End If
End Function).Take(MyInteger.Length - 1).ToArray()


            Dim level As Integer
            level += 1
            For Each Itm In ResultStr
                Dim intStart, IntEnd As Integer
                intStart = Val(Split(Itm, "-")(0))
                IntEnd = Val(Split(Itm, "-")(1))
                Dim sttMid As String = Mid(BodyText, intStart, Math.Abs(IntEnd - intStart))
                Dim stt4 As String = MultiFinderString(sttMid, MultiFind(level))
                Return stt4
                LisCheck.Add(sttMid)
            Next
        End If
    End Function

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,743 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,451 Reputation points Microsoft Vendor
    2024-11-05T06:24:04.03+00:00

    Hi @Mansour_Dalir ,

    If you want to access the right result, you need to make trade-offs after each split, not at the last one.

        Function MultiFinderString(BodyText As String, MultiFind As List(Of String)) As String()
            Return MultiFind.Aggregate({BodyText},
                                       Function(result, item) result.
                                           SelectMany(Function(str) str.Split(item)).
                                           Where(Function(str) MultiFind.IndexOf(item) = MultiFind.Count - 1 OrElse str.Contains(MultiFind(MultiFind.IndexOf(item) + 1))).
                                           ToArray()
                                      )
        End Function
    
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Q&A Assist
    2024-11-02T05:47:54.5366667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To achieve multi-splitting of the text based on different levels of delimiters in Visual Basic, you can modify your approach to handle each delimiter separately and recursively. The provided code snippet is a good starting point, but it needs adjustments to correctly return the expected output.

    Here’s a revised version of your function that processes the string based on multiple delimiters:

    Private Function MultiFinderString(BodyText As String, find As String, Optional MultiFind As String() = Nothing) As List(Of String)
        Dim result As New List(Of String)()
        Dim position As Integer = InStr(BodyText, find)
    
        While position > 0
            Dim startPos As Integer = position - 1
            Dim endPos As Integer = position + Len(find) - 1
    
            ' Capture the substring around the found delimiter
            If startPos >= 0 And endPos < BodyText.Length Then
                result.Add(Mid(BodyText, startPos, endPos - startPos + 1))
            End If
    
            position = InStr(position + 1, BodyText, find)
        End While
    
        ' Recursively process for the next level of delimiters
        If MultiFind IsNot Nothing AndAlso MultiFind.Length > 1 Then
            Dim nextLevelResults As New List(Of String)()
            For Each item In result
                Dim nextResults = MultiFinderString(item, MultiFind(1), MultiFind.Skip(1).ToArray())
                nextLevelResults.AddRange(nextResults)
            Next
            Return nextLevelResults
        End If
    
        Return result
    End Function
    

    In this function:

    • It searches for the specified delimiter and captures the surrounding text.
    • It recursively processes the found substrings for the next level of delimiters.
    • The results are collected and returned as a list of strings.

    You can call this function with your initial string and the array of delimiters to get the desired output.


    References:

    0 comments No comments

  2. Mansour_Dalir 1,976 Reputation points
    2024-11-02T07:21:23.2033333+00:00
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim MyString As String = "00[A]1111{C}22(B)33333{C}4{C}5{C}666[A]777(B)8(B)99{C}999[A]101010"
            Dim LevelSplit As New List(Of String)
            LevelSplit.AddRange({"A", "B", "C"})
            Dim Result As String() = MultiFinderString(MyString, LevelSplit)
        End Sub
        Function MultiFinderString(BodyText As String, MultiFind As List(Of String)) As String()
            Dim Seprator As Char = Microsoft.VisualBasic.Chr(3)
            For Each item In MultiFind
                Dim idx As Integer = MultiFind.IndexOf(item)
                If idx = MultiFind.Count - 1 Then
                    Dim sttSetup As String = Join(BodyText.Split(Seprator).Where(Function(f) f Like "*" & item & "*").Select(Function(f) f).ToArray, item)
                    Return Split(sttSetup, item)
                Else
                    BodyText = Replace(BodyText, item, Seprator)
                End If
            Next
        End Function
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.