我有一个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
结果: