1

How, using C#, can I create a new string[] from another string[] containing a subsection of the old string[] array?

For example, if I had:

string[] a = { "cat", "dog", "hamster", "parrot" }

And I wanted to get everything after the first element I should get:

string[] b = { "dog", "hamster", "parrot" }

How, if this is even possible without a for loop, do I achieve this?

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
TechnoCF
  • 178
  • 1
  • 8

1 Answers1

5

You can use some extensions methods to solve it, that is the case for Skip(int) under the System.Linq namespace, for sample:

string[] b = a.Skip(1).ToArray();

And it will skip 1 element and convert the result into a new array. Remember to add the namespace:

using System.Linq;

As the MSDN documentation said:

Bypasses a specified number of elements in a sequence and then returns the remaining elements.

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194