user5599599
user5599599

Reputation:

Convert Array to string from special index

I have a simple question.

I have an string array of length 5 and want to convert it to a string. But I am interested in to convert from a specified index(for example from Array_temp[2] ) till end of array.

I know the following code will do it for whole array not part of it. Could please one help me how can I do it?

string.Join("/", Array_temp)

Upvotes: 0

Views: 886

Answers (2)

Matthew Watson
Matthew Watson

Reputation: 109862

You can use the overload of string.Join() that lets you specify an offset and a count:

string[] Array_temp = {"1", "2", "3", "4", "5"};
int offset = 2;
var result = string.Join("/", Array_temp, offset, Array_temp.Length - offset);

Console.WriteLine(result); // 3/4/5

Note that this is somewhat faster than the version that accepts an IEnumerable<string> because it has some optimisations arising from the fact that it knows in advance how many strings there are.

Upvotes: 0

Manfred Radlwimmer
Manfred Radlwimmer

Reputation: 13404

You can use the Skip method of Linq:

string.Join("/", Array_temp.Skip(2));

Skip will return an IEnumerable of whatever you called it on and skip the first x (2 in this example) entries.

Upvotes: 6

Related Questions