不区分大小写的“包含(字符串)Contains(string)”

有没有办法让下面的结果为真?

string title = "ASTRINGTOTEST";
title.Contains("string");

似乎没有允许我设置区分大小写的重载..目前我把它们都大写,但这只是愚蠢的(我指的是 i18n 上下大小写问题)。

更新
这个问题很古老,从那时起我就意识到,如果您愿意对一个非常庞大而困难的话题进行全面调查,我会要求一个简单的答案。
在大多数情况下,在单语、英语代码库中这个答案就足够了。 我怀疑是因为大多数来这里的人都属于这一类,这是最受欢迎的答案。
这个答案提出了一个固有问题,即在我们知道两个文本是相同的文化之前,我们无法比较不区分大小写的文本 我们知道这种文化是什么。 这可能是一个不太受欢迎的答案,但我认为它更正确,这就是我将其标记为这样的原因。

You could always just up or downcase the strings first.

string title = "string":
title.ToUpper().Contains("STRING")  // returns true

Oops, just saw that last bit. A case insensitive compare would *probably* do the same anyway, and if performance is not an issue, I don't see a problem with creating uppercase copies and comparing those. I could have sworn that I once saw a case-insensitive compare once...

OrdinalIgnoreCase, CurrentCultureIgnoreCase or InvariantCultureIgnoreCase?

Since this is missing, here are some recommendations about when to use which one:

Dos

  • Use StringComparison.OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.
  • Use StringComparison.OrdinalIgnoreCase comparisons for increased speed.
  • Use StringComparison.CurrentCulture-based string operations when displaying the output to the user.
  • Switch current use of string operations based on the invariant culture to use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when the comparison is
    linguistically irrelevant (symbolic, for example).
  • Use ToUpperInvariant rather than ToLowerInvariant when normalizing strings for comparison.

Don'ts

  • Use overloads for string operations that don't explicitly or implicitly specify the string comparison mechanism.
  • Use StringComparison.InvariantCulture -based string
    operations in most cases; one of the few exceptions would be
    persisting linguistically meaningful but culturally-agnostic data.

Based on these rules you should use:

string title = "STRING";
if (title.IndexOf("string", 0, StringComparison.[YourDecision]) != -1)
{
    // The string exists in the original
}

whereas [YourDecision] depends on the recommendations from above.

link of source: http://msdn.microsoft.com/en-us/library/ms973919.aspx

Just to build on the answer here, you can create a string extension method to make this a little more user-friendly:

    public static bool ContainsIgnoreCase(this string paragraph, string word)
    {
        return CultureInfo.CurrentCulture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0;
    }
</div>