Is there a more concise way to extract (n > i) elements from an array?

  • A+
Category:Languages

I need a piece of code that is capable of taking an array of strings of length l, extracting all of the elements that are present after i, where i is a particular index less than l and then placing the extracted elements into a new array.

Essentially if we have the below:

["A", "B", "C", "D", "E", "F"]

In this instance l = 6 and if we take n = 3 then the code should create a new array

["D", "E", "F"]

I've written the code out to do this, but I can't help think there's a better, more concise way to go about it.

Here's what I have:

List<string> tempElements = new List<string>();  for (int i = startOfMessage; i < elements.Length; i++) {     tempElements.Add(elements[i]); }  string[] usableMessageElements = tempElements.ToArray(); 

A relatively simple problem, but I'm aiming to reduce as much junior code as I can.

 


It sounds like you just want to use Skip from LINQ:

string[] usableMessageElements = elements.Skip(startOfMessage).ToArray(); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: