Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way get first word and rest of the words in a string in C#

Tags:

string

c#

linq

In C#

var parameters =
    from line in parameterTextBox.Lines
    select new {name = line.Split(' ').First(), value = line.Split(' ').Skip(1)};

Is there a way to do this without having to split twice?

like image 689
CookieOfFortune Avatar asked Aug 02 '10 16:08

CookieOfFortune


2 Answers

you can store the split in a let clause

var parameters =
    from line in parameterTextBox.Lines
    let split = line.Split(' ')
    select new {name = split.First(), value = split.Skip(1)};
like image 64
Matt Greer Avatar answered Sep 26 '22 23:09

Matt Greer


Sure.

var parameters = from line in parameterTextBox.Lines
                 let words = line.Split(' ')
                 select new { name = words.First(), words.skip(1) };
like image 26
SolutionYogi Avatar answered Sep 25 '22 23:09

SolutionYogi