How to get sub-array from larger array in VB.NET?

后端 未结 3 1304
梦毁少年i
梦毁少年i 2021-01-13 23:06

I have:

Dim arr() As String = {\"one\",\"two\",\"three\"}

I want a new array, sub, containing {\"one\", \"three\"} only. What

3条回答
  •  醉话见心
    2021-01-13 23:30

    For this particular case, the simplest option is just to list the two items you want to copy:

    Dim sub = {arr(0), arr(2)}
    

    In the general case, if you want to take the first item, skip one item, and then take all the rest, an easy option would be to use the LINQ extension methods:

    Dim sub = arr.Take(1).Concat(arr.Skip(2)).ToArray()
    

    It yields

    • {"one"} (arr.Take(1))
    • concatenated with (Concat)
    • {"three"} (arr.Skip(2))
    • into a new array (ToArray())

    Documentation:

    • Enumerable.Take
    • Enumerable.Skip

提交回复
热议问题