将范围从一个列表添加到另一个列表

Add Range from one list to another

我有一个 list(of string),我搜索它以获得开始和结束范围,然后我需要将该范围添加到单独的列表中

例如:列表 A = "a" "ab" "abc" "ba" "bac" "bdb" "cba" "zba"

我需要列表 B 是所有 b 的 (3-5)

我想做的是ListB.Addrange(ListA(3-5))
我怎样才能做到这一点??

使用List.GetRange()

Imports System
Imports System.Collections.Generic

Sub Main()
    '                                               0    1     2      3     4      5      6      7
    Dim ListA As New List(Of String)(New String() {"a", "ab", "abc", "ba", "bac", "bdb", "cba", "zba"})
    Dim ListB As New List(Of String)

    ListB.AddRange(ListA.GetRange(3, 3))
    For Each Str As String In ListB
        Console.WriteLine(Str)
    Next
    Console.ReadLine()
End Sub

或者您可以使用 Linq

Imports System
Imports System.Collections.Generic
Imports System.Linq

Module Module1
    Sub Main()
        '                                               0    1     2      3     4      5      6      7
        Dim ListA As New List(Of String)(New String() {"a", "ab", "abc", "ba", "bac", "bdb", "cba", "zba"})
        Dim ListB As New List(Of String)

        ListB.AddRange(ListA.Where(Function(s) s.StartsWith("b")))
        ' This does the same thing as .Where()
        ' ListB.AddRange(ListA.FindAll(Function(s) s.StartsWith("b")))
        For Each Str As String In ListB
            Console.WriteLine(Str)
        Next
        Console.ReadLine()
    End Sub
End Module

结果: