Search code examples
c#.netlinqforeachskip

skip multiple values in foreach loop


I have following string array, and I'm getting its as string in foreach loop

string[] words = ...

foreach (String W in words.Skip(1))
{
      ...
}

I can skip first value but How can I skip 1st value and last value both ?


Solution

  • Try this

    foreach (string w in words.Skip(1).Take(words.length-2))
    {
        ...
    }
    

    Probably best to put some tests before this to ensure there are enough words!