我有一个名为 dictionaryWords 的对象集合。
对于这个集合中的每个单词,我需要检查它是否不包含某些字母。如果它确实包含一个或多个特定字母,则会将其从集合中删除。
例子:
Collection before removal: ['abc','dee',fff']
letters to check for: e,f
Collection after removal: ['abc']
有没有一种方法可以检查数组而不是指定多个字母?
我的代码:
foreach(DictionaryWord word in dictionaryWords)
{
if (!word.Contains("D") && !word.Contains("E") // optimize this line
{
// Word does not contain letters, word is good
}
}
如何将“优化此行”替换为“如果单词包含值数组中的任何字母”
谢谢, 安德鲁
最佳答案
尝试这样的事情:
// isolate the letters
string[] letters = new string[] { "D", "E", "F" }; // other strings or letters
// interate between your data
foreach(DictionaryWord word in dictionaryWords)
{
// check if the work does not contain any letter
if (!letters.Any(x => word.Contains(x))
{
// Word does not contain letters, word is good
}
}
关于c# - 检查字符串是否包含多个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21706770/