c# - 检查字符串是否包含多个字母

标签 c#

我有一个名为 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/

相关文章:

C# 压缩文件。如何提取或跳过名称中包含无效字符的文件?

c# - iTextSharp 设置默认字体大小

C# 多个正则表达式替换字符串 - 内存太多

c# - Outlook Com 未注册

c# - 从 google places api 反序列化 json

c# - 系统.InvalidOperationException : Cannot attach an entity that already exists

c# - 如何以编程方式更改 WMI 的设置而不是使用 wmimgmt.msc snappin?

c# - Excel: boolean 值的本地名称c#

c# - 监听文件系统

C#:为什么 ShowDialog().ToString() 不返回预期的字符串?