2
我有一個list(of string)
和我搜索它得到一個開始和結束範圍,那麼我就需要這個範圍內添加到一個單獨的列表從一個列表添加範圍到另一個
前:名單A =「A」「 AB」, 「ABC」, 「巴」, 「脊樑」 「BDB」 「CBA」 「零餘額賬戶」
我需要列表b是所有的b(3-5)
我想要做的是ListB.Addrange(ListA(3-5))
我該如何做到這一點?
我有一個list(of string)
和我搜索它得到一個開始和結束範圍,那麼我就需要這個範圍內添加到一個單獨的列表從一個列表添加範圍到另一個
前:名單A =「A」「 AB」, 「ABC」, 「巴」, 「脊樑」 「BDB」 「CBA」 「零餘額賬戶」
我需要列表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
結果:
.GetRange(開始,結束開始)爲我工作 – Dman
搜索鍵是:'.FindAll'和'.CopyTo' – Muhammad