0

I have a string array with 31 strings, now I have to extract string from 3rd position to 27th position and pass that part as an string array to another function. how do I accomplish this in c#?

user3400453
  • 21
  • 1
  • 6
  • 1
    Possible duplicate of http://stackoverflow.com/questions/943635/c-sharp-arrays-getting-a-sub-array-from-an-existing-array or http://stackoverflow.com/questions/406485/array-slices-in-c-sharp – nonsensickle Mar 24 '14 at 07:47

5 Answers5

2

You can use Array.Copy(Array, Int32, Array, Int32, Int32) overload ;

Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index.

Array.Copy(source, 2, destination, 0, 25);

But this create a new array. You can use LINQ istead with Skip and Take methods like;

var destination = source.Skip(2).Take(25).ToArray();

I assume you want 27th position also, that's why I used 25 as a length (or count) in both example. If you don't want to get 27th position, you need to change 25 to 24 in both case.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

If you have linq you can do something like:

var finalResult = foo(myStrings.Skip(2).Take(25).ToArray())
Einer
  • 146
  • 4
0

Try Like this

  var arr2 = arr1.Select( x => x.substring(3,27));
Sathish
  • 4,419
  • 4
  • 30
  • 59
blorkfish
  • 21,800
  • 4
  • 33
  • 24
0

The answer is very simple if you use LINQ.

var arrayToPassToMethod = array.Skip(2).Take(24).ToArray();
Michael Mairegger
  • 6,833
  • 28
  • 41
0

Try this:

string[][] result = a.Skip(2).Take(24).ToArray<string[]>();
Oscar Bralo
  • 1,912
  • 13
  • 12