Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Index of a String Array after Split using LINQ

Tags:

c#

lambda

linq

I am trying to get the third index of my splitted string. But I cannot get the exact value using LINQ. I am trying to get the third index value which is "CC":

string strInput = @"AA BB CC DD EE";
var xRes = strInput.Split(' ').Skip(1).Take(1).Select(c => c).ToArray();

The last line was able to get the exact third array. But I wasn't able to convert it to string. If I do this:

var xRes = strInput.Split(' ').Skip(2).Take(1).Select(c => c[0].ToString()).ToString();

I get this instead:

System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.String,System.String]

like image 367
Willy David Jr Avatar asked May 31 '26 12:05

Willy David Jr


1 Answers

How about

string strInput = @"AA BB CC DD EE";
var xRes = strInput.Split(' ')[2];

You don't need to use LINQ to do that.

If you insist in using LINQ, you can do it using ElementAt.

var xRes = strInput.Split(' ').ElementAt(2);

Or Skip followed by First

var xRes = strInput.Split(' ').Skip(2).First();
like image 59
Niyoko Avatar answered Jun 02 '26 02:06

Niyoko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!