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#?
Asked
Active
Viewed 2,780 times
0
-
1Possible 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 Answers
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
-
This is not OP wants. This takes 27 elements after third element. OP wants to get 3rd position to 27th position. – Soner Gönül Mar 24 '14 at 08:25
0
Try Like this
var arr2 = arr1.Select( x => x.substring(3,27));
-
-
List
arr1 = new List – blorkfish Mar 24 '14 at 07:53{ "test1", "test2", "test3" }, then the above will work.
0
The answer is very simple if you use LINQ.
var arrayToPassToMethod = array.Skip(2).Take(24).ToArray();

Michael Mairegger
- 6,833
- 28
- 41