Home > Net >  Start a for each loop after a certain string
Start a for each loop after a certain string

Time:07-22

I need an example on how to find a particular string after the for each loop below. For example, I have the for each below that finds "Dtq" in the "Note field." When it finishes, I need to then find another string after that, "ABC". But it needs to start the second loop for "ABC" after the last "Dtq" in that field not before it. If I have a string "Dtq, ABC, Dtq, ABC" for example, I need to loop and get all the "ABC" after the last find of "Dtq" string. Basically, start a loop to find "ABC" after the first loop finishes finding the last "Dtq" string. Is this possible?

my for each statement:

    For Each LogEntry As DataRowView In orderedLog
        If InStr(LogEntry("Note").ToString, "Dtq") > 0 Then
            TqTechnique = TqTechnique & LogEntry("Note").substring(Len("Dtq:"))
            TqFound = True
           
        End If
    Next

CodePudding user response:

You could use a normal for loop using the indexer of the first loop to start the second loop

' First loop with index
Dim idxFirst As Integer
For idxFirst = 0 To orderedLog.Count - 1
    ' Set the entry to look for from the orderedLog array/list
    Dim LogEntry As DataRowView = orderedLog(idxFirst)
    If InStr(LogEntry("Note").ToString, "Dtq") > 0 Then
        TqTechnique = TqTechnique & LogEntry("Note").substring(Len("Dtq:"))
        TqFound = True
        ' Stop the loop with the flag found
        Exit For
    End If
Next


If  TqFound Then
        
    Dim idxSecond as Integer

    ' Start from the row after the first successful search
    For idxSecond = idxFirst   1 To orderedLog.Count - 1
        Dim LogEntry2 As DataRowView = orderedLog(idxSecond)
        ' Start your search for the second string
        .....
    Next
End If
  • Related