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#?

1

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

If you have linq you can do something like:

var finalResult = foo(myStrings.Skip(2).Take(25).ToArray())

1 Comment

This is not OP wants. This takes 27 elements after third element. OP wants to get 3rd position to 27th position.
0

The answer is very simple if you use LINQ.

var arrayToPassToMethod = array.Skip(2).Take(24).ToArray();

Comments

0

Try this:

string[][] result = a.Skip(2).Take(24).ToArray<string[]>();

Comments

0

Try Like this

  var arr2 = arr1.Select( x => x.substring(3,27));

2 Comments

Erm, no. It's not one string, but an array of strings.
List<string> arr1 = new List<string> { "test1", "test2", "test3" }, then the above will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.