We introduce a function EveryNthElement, which receives a List of strings, and returns another List of strings. The result is a collection containing the specified elements.
Module Module1
Function EveryNthElement(list As List(Of String), n As Integer) As List(Of String)
Dim result = New List(Of String)
' Step 1: loop over all indexes.
For i As Integer = 0 To list.Count - 1
' Step 2: use a modulo expression, and add values if the result is 0.
If i Mod n = 0
result.Add(list(i))
End If
Next
' Step 3: return the list.
Return result
End Function
Sub Main()
' Use the function on this list.
Dim test = New List(Of String)({
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i" })
Dim result = EveryNthElement(test, 2)
Console.WriteLine(string.Join(
",", result))
Dim result2 = EveryNthElement(test, 3)
Console.WriteLine(string.Join(
",", result2))
End Sub
End Module
a,c,e,g,i
a,d,g