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]
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With