Search code examples
c#listreorderlist

extracting specific format elements from the list in c#


how to separate (numeric elements and alphabetic elements) with (alphanumeric elements and elements with brackets like [Constant 0.001]) from a list into two separate lists.

like we have elements in

list<string> abc={'123','11','abcc','abdd','abcd11','abcd12',[abcd]}

after seprating elements in two lists..

list<string> abc1={'123','11','abcc','abdd'}  
list<string> abc2={'abcd11','abcd12',[abcd]}

Solution

  • var abc = new List<string> { "abc", "123", "abd12" };
    
    var alphaXorNumerical = abc.Where(str => str.All(Char.IsDigit) ||
                                             str.All(Char.IsLetter));
    
    var others = abc.Except(alphaXorNumerical);
    

    If you also want to check for whitespace, use this instead:

    var alphaXorNumerical = abc
              .Where(str => str.All(Char.IsDigit) ||
                            str.All(ch => Char.IsLetter(ch) || Char.IsWhiteSpace(ch)));