Reputation: 1953
I want to loop through a string two lines at a time. I know I can loop through each line in a string by splitting on carriage return with the following:
For Each line As String In Split(myString, vbCrLf)
//do something with line
Continue For
End If
How might I iterate through a string two lines at a time? Do I have to use two loops?
Upvotes: 0
Views: 52
Reputation: 3271
You can't do the loop you are after with a For..Each
loop as the definition of that loop is to loop through each element.
You need to use an old-fashioned For
loop with a counter and a Step
instruction.
Dim stringArray As String() = Split(myString, vbCrLf)
For loopCounter As Integer = 0 To stringArray.Length - 2 Step 2
If (loopCounter + 2 >= stringArray.Length) Then
' Need to handle the scenario for an Odd number of items in the array
Debug.WriteLine($"{stringArray(loopCounter)}")
Else
Debug.WriteLine($"{stringArray(loopCounter)}:{stringArray(loopCounter + 1)}")
End If
Next
Upvotes: 1