I have:
Dim arr() As String = {"one","two","three"}
I want a new array, sub
, containing {"one", "three"} only. What is the best method to do this?
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)
) Concat
){"three"}
(arr.Skip(2)
)ToArray()
)Documentation:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With