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#?
5 Answers
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.
Comments
If you have linq you can do something like:
var finalResult = foo(myStrings.Skip(2).Take(25).ToArray())
1 Comment
Soner Gönül
This is not OP wants. This takes 27 elements after third element. OP wants to get 3rd position to 27th position.
Try Like this
var arr2 = arr1.Select( x => x.substring(3,27));
2 Comments
Thorsten Dittmar
Erm, no. It's not one string, but an array of strings.
blorkfish
List<string> arr1 = new List<string> { "test1", "test2", "test3" }, then the above will work.