I have a string[]
, and want to get the elements of the string[]
that has an index, of which i know exist, specified in an int[]
.
string[] stringArray = { "a", "b", "c", "d", "e", "f", "g" };
int[] indices = { 1, 2, 4, 6 };
From this, I am trying to get a string[]
containing { "b", "c", "e", "g" }
. Preferably using a lambda expression. How would I do this?
indices.Select(i => stringArray[i]);
One way you can do it is like this.
string[] result = indices.Select(i => stringArray[i]).ToArray()
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