2013-03-20 44 views
0

我正在寻找一种聪明的方法来在字符串列表中查找带有特定字符的列表元素。查找字符串列表中的字符

我想找到的所有字符串,至极包含像字符串列表 - 符号: “?一个11”, “AB12”, “BB12”, “2 B 13”

我现在解决方案是这样的:

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

是否有更好的方法来完成这项工作,也许像:

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

任何想法?

+1

更好是非常相对的。我会尝试codereview.SE。 – 2013-03-20 22:13:04

回答

5
listOfStrings.Where(s => s.Contains('?')); 
+0

就这样简单。 :) 我尝试了FindAll而不是Where和它的作品。 有什么区别吗? 非常感谢! – 2013-03-20 22:26:48

+1

@ChristophBrückmann对这里的区别有一个很好的解释http://stackoverflow.com/questions/1531702/findall-vs-where-extension-method – keyboardP 2013-03-20 23:01:50