我有一个list(of string),我搜索它以获得一个开始和结束范围,然后我需要将这个范围添加到一个单独的列表中
例:列表A= "a“ab”abc“"ba”"bac“"bdb”"cba“"zba”
我需要名单B是所有的b (3-5)
我想做的是ListB.Addrange(ListA(3-5))
我怎样才能做到这一点?
发布于 2015-05-11 16:12:12
使用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结果:

https://stackoverflow.com/questions/30172407
复制相似问题