Get all values between two specific values in a list

Nig*_*ape 0 c# linq char

I have a char list:

var myList = new List<char>(new[] { 'x', 'y', '(', 'a', 'b', 'c', ')', 'z' });
Run Code Online (Sandbox Code Playgroud)

With the values:

 'x'
 'y'
 '('
 'a'
 'b'
 'c'
 ')'
 'z' 
Run Code Online (Sandbox Code Playgroud)

How can i take all the values between the braces? The values in new list should look like this:

 'a'
 'b'
 'c'
Run Code Online (Sandbox Code Playgroud)

The index of the braces in the list can change every session.

ang*_*son 5

You can use this LINQ expression:

myList.SkipWhile(c => c != '(').Skip(1).TakeWhile(c => c != ')')
       ^----- 1 --------------^ ^- 2 -^ ^-------- 3 -----------^
Run Code Online (Sandbox Code Playgroud)
  1. Skip until we find the (
  2. Skip past the ( we found
  3. Take elements until we find the ) (which will not be included)

Notes:

  1. If the sequence contains no starting parenthesis, an empty sequence will be returned
  2. If the sequence contains no matching end parenthesis, the rest of the sequence will be returned