How to split a string with LINQ and get the specified token

I have a tab delimited string in the following format:

string line = "D\t892270\t88418\t6\t2452927\t-99999\t-99999\t12";

I want to get the value of the token specified through indexToGet.

I wrote the following LINQ query for this, which I am sure can be improved, but at the moment I can not do this. Can anyone improve this LINQ query for me?

int indexToGet = 7;
var val =
    (from str in line.Split('\t')
    select str).ToArray().GetValue(indexToGet).ToString();
+3
source share
2 answers

Why do you need to use LINQ? The following code solves it without LINQ overhead:

var val = line.Split('\t')[indexToGet];

: , , , . , LINQ ElementAt. , .ToArray().GetValue(indexToGet).ToString(); :

var val =
    (from str in line.Split('\t')
    select str).ElementAt(indexToGet);

, , , ( ), :

var val = line.Split('\t').ElementAt(indexToGet);

, ElementAt , :

var val = line.Split('\t')[indexToGet];
+6

LINQ.

(from str in line.Split('\t') select str).ToArray()

line.Split('\t')

- , , . , .

, , , .ToString().

+2

Source: https://habr.com/ru/post/1772604/


All Articles