FadelMS
FadelMS

Reputation: 2037

dividing a string array in groups

I have a string array of 3000 words. How can I divide the array into groups of ten using LINQ. Each ten items should be stored in one variable. The result should be a new array containing the groups.

Upvotes: 2

Views: 150

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726609

Assuming that the words are separated by single space, you can split and re-group like this:

var res = longWord
    .Split(' ').
    .Select((s, i) => new { Str = s, Index = i })
    .GroupBy(p => p.Index / 10)
    .Select(g => string.Join(" ", g.Select(v => v.Str)));

Upvotes: 1

Related Questions