【问题标题】:Add Range from one list to another将范围从一个列表添加到另一个列表
【发布时间】:2015-07-22 05:41:24
【问题描述】:

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

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

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

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

【问题讨论】:

  • 搜索关键字是:.FindAll.CopyTo

标签: vb.net list addrange


【解决方案1】:

使用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

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2020-01-23
    • 2014-03-14
    • 1970-01-01
    • 2022-06-13
    • 2017-03-02
    相关资源
    最近更新 更多