0

I'm looking for a smart way to find List-Elements with a specific Character in a List of Strings.

I want to find all Strings, wich contains a ?-sign in a List of Strings like: "a?11", "ab12", "bb12", "b?13"

My current solution looks like this:

// Interates through all strings.
foreach (string currentString in listOfStrings)
{
  if (currentString.Contains('?'))
  {
    // Found!
    myStrings.Add(currentString);
  }
}

Is there a better way to do this job, maybe like:

List<string> myStrings = listOfStrings.Select(z => z.Contains('?')).ToList();

Any ideas?

Christoph Brückmann
  • 1,373
  • 1
  • 23
  • 41

1 Answers1

5
listOfStrings.Where(s => s.Contains('?'));
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Simple as it is. :) I tryed FindAll instead of Where and it works too. Are there any differences? Thank you very much! – Christoph Brückmann Mar 20 '13 at 22:26
  • 1
    @ChristophBrückmann There's a good explanation of the differences here http://stackoverflow.com/questions/1531702/findall-vs-where-extension-method – keyboardP Mar 20 '13 at 23:01