How to Get the Positions of Strings Filtered by Multi-Split Method in VB.net With LINQ

Mansour_Dalir 1,976 Reputation points
2024-11-05T08:06:54.8233333+00:00

According to the previous question, the method of multi-splitting (Multi-Split)

I'm working on a VB.net script and need help extracting the positions of specific substrings from a given input string based on a multi-split condition. The input string is defined as follows:

Dim MultiSplit as String()={"C","B","A"}
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 a string array containing the positions of each occurrence of the substrings. For example, for MultiSplit containing {"C", "B", "A"}, the expected output for positions is:

Dim ResultOfPositions as String()={"32-36","38-42"}.

  • For {"A", "B", "C"}, the result should be Dim ResultOfPositions as String()={"4-9","11-14","16-22","24-26","28-30","31-35","48-51","53-57"}.

Any guidance on how to achieve this would be appreciated!

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,456 Reputation points Microsoft Vendor
    2024-11-05T08:20:59.8733333+00:00

    Hi @Mansour_Dalir ,

    To get the position of a substring in the original string, you can use the String.IndexOf method.

        Function MultiFinderString(BodyText As String, MultiFind As List(Of String)) As String()
            Dim result = New List(Of String) From {BodyText}
            Dim p As Integer = 0  ' Start position for searching
    
            For i = 0 To MultiFind.Count - 1
                Dim item = MultiFind(i)
                result = result.
                    
                    Where(Function(part) i = MultiFind.Count - 1 OrElse part.Contains(MultiFind(i + 1))).
                    ToList()
            Next
    
            Dim positions = result.
                Select(Function(part)
                           Dim startIndex = BodyText.IndexOf(part, p)
                           Dim endIndex = startIndex + part.Length - 1
                           p = endIndex + 1  ' Move start position to after the found part
                           Return $"{startIndex}-{endIndex}"
                       End Function).
                ToArray()
    
            Return positions
        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.

0 additional answers

Sort by: Most helpful

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.