我有一个包含一串字符串的列表testList。我想添加一个新的字符串到testList中,如果它还不存在于列表中。因此,我需要对列表进行不区分大小写的搜索,并使其高效。我不能使用Contains,因为它没有考虑到外壳。出于性能原因,我也不想使用ToUpper/ToLower。我发现了这个方法,它很有效:

    if(testList.FindAll(x => x.IndexOf(keyword, 
                       StringComparison.OrdinalIgnoreCase) >= 0).Count > 0)
       Console.WriteLine("Found in list");

这是可行的,但它也匹配部分单词。如果列表包含“goat”,我就不能添加“oat”,因为它声称“oat”已经在列表中。有没有一种方法可以有效地搜索列表,不区分大小写,其中单词必须完全匹配?谢谢


当前回答

我知道这是一篇旧文章,但为了防止其他人看到,你可以通过提供不区分大小写的字符串相等比较器来使用Contains,就像这样:

using System.Linq;

// ...

if (testList.Contains(keyword, StringComparer.OrdinalIgnoreCase))
{
    Console.WriteLine("Keyword Exists");
}

从。net 2.0开始就有了。

其他回答

而不是字符串。IndexOf,使用String。等于以确保没有部分匹配。另外,不要使用FindAll,因为它遍历每个元素,而是使用FindIndex(它会在遇到第一个元素时停止)。

if(testList.FindIndex(x => x.Equals(keyword,  
    StringComparison.OrdinalIgnoreCase) ) != -1) 
    Console.WriteLine("Found in list"); 

或者使用一些LINQ方法(它也会在它碰到的第一个方法时停止)

if( testList.Any( s => s.Equals(keyword, StringComparison.OrdinalIgnoreCase) ) )
    Console.WriteLine("found in list");

基于Lance Larsen的回答-这里有一个推荐字符串的扩展方法。比较而不是字符串。=

强烈建议您使用String的重载。它接受一个stringcompare参数。这些重载不仅允许您定义您想要的准确比较行为,使用它们还将使您的代码对其他开发人员更具可读性。[Josh Free @ BCL团队博客]

public static bool Contains(this List<string> source, string toCheck, StringComparison comp)
{
    return
       source != null &&
       !string.IsNullOrEmpty(toCheck) &&
       source.Any(x => string.Compare(x, toCheck, comp) == 0);
}

您正在检查IndexOf的结果是否大于或等于0,这意味着匹配是否从字符串的任何位置开始。试着检查它是否等于0:

if (testList.FindAll(x => x.IndexOf(keyword, 
                   StringComparison.OrdinalIgnoreCase) >= 0).Count > 0)
   Console.WriteLine("Found in list");

“goat”和“oat”不匹配,但“goat”和“goa”可以。为了避免这种情况,可以比较两个字符串的长度。

为了避免所有这些复杂情况,可以使用字典而不是列表。键是小写字符串,值是实字符串。这样,性能不会受到影响,因为您不必为每个比较使用ToLower,但您仍然可以使用Contains。

例如,你可以从LINQ中使用Contains重载StringComparer静态变量,如下所示:

using System.Linq;

var list = new List<string>();
list.Add("cat");
list.Add("dog");
list.Add("moth");

if (list.Contains("MOTH", StringComparer.OrdinalIgnoreCase))
{
    Console.WriteLine("found");
}

基于Adam Sills上面的回答——这里有一个很好的、干净的Contains扩展方法…:)

///----------------------------------------------------------------------
/// <summary>
/// Determines whether the specified list contains the matching string value
/// </summary>
/// <param name="list">The list.</param>
/// <param name="value">The value to match.</param>
/// <param name="ignoreCase">if set to <c>true</c> the case is ignored.</param>
/// <returns>
///   <c>true</c> if the specified list contais the matching string; otherwise, <c>false</c>.
/// </returns>
///----------------------------------------------------------------------
public static bool Contains(this List<string> list, string value, bool ignoreCase = false)
{
    return ignoreCase ?
        list.Any(s => s.Equals(value, StringComparison.OrdinalIgnoreCase)) :
        list.Contains(value);
}