我有一个 For Each 循环,它循环遍历字符串数组以查找长度为三个字符的第一个字符串。例如,当循环数组时,如果前 3 个字符串是数组的第四个索引,我希望它返回数组的第三个(前一个)索引。任何帮助将不胜感激。
For Each piece As String In p
If piece.Length = 3 Then
'Return previous index
End If
Next
最佳答案
您有几个选择。
如果p
是可通过索引访问的集合(即:IList(Of T)
),则可以切换到For循环,并返回当前位置的元素索引-1。
否则,您可以将前一项保留在单独的变量中,并在满足条件时返回它。
Dim lastItem as String
For Each piece As String In p
If piece.Length = 3 Then
Return lastItem ' Will return Nothing if this is the first item...
End If
lastItem = piece ' Store this for next time
Next
关于vb.net - 返回 vb.net 中的前一个索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347770/