有没有一种方法可以实现以下返回?

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

似乎没有一个过载允许我设置大小写敏感度。目前,我对这两个问题都有夸大的感觉,但这太傻了(我指的是大小写中的i18n问题)。

更新

这是一个古老的问题,从那时起,我意识到,如果你愿意充分调查,我要求一个非常广泛和困难的问题的简单答案。

对于大多数情况,在单语言、英语代码基础上,这个答案就足够了。我怀疑,因为大多数来这里的人都属于这一类,这是最流行的答案。

然而,这个答案带来了一个固有的问题,即在我们知道两个文本都是相同的文化并且我们知道文化是什么之前,我们不能比较文本不区分大小写。这可能是一个不太流行的答案,但我认为它更正确,这就是我将其标记为这样的原因。


当前回答

.NET Core 2.0+(包括.NET 5.0+)

自2.0版以来,.NET Core有两种方法来处理此问题:

String.Contains(Char,StringComparison)String.Contains(字符串,StringComparison)

例子:

"Test".Contains("test", System.StringComparison.CurrentCultureIgnoreCase);

它现在正式成为.NET标准2.1的一部分,因此也是实现此版本标准(或更高版本)的基类库的所有实现的一部分。

其他回答

您可以使用字符串比较参数(可从.NET Core 2.1及更高版本获得)string.Contains方法。

public bool Contains (string value, StringComparison comparisonType);

例子:

string title = "ASTRINGTOTEST";
title.Contains("string", StringComparison.InvariantCultureIgnoreCase);
if ("strcmpstring1".IndexOf(Convert.ToString("strcmpstring2"), StringComparison.CurrentCultureIgnoreCase) >= 0){return true;}else{return false;}
public static class StringExtension
{
    #region Public Methods

    public static bool ExContains(this string fullText, string value)
    {
        return ExIndexOf(fullText, value) > -1;
    }

    public static bool ExEquals(this string text, string textToCompare)
    {
        return text.Equals(textToCompare, StringComparison.OrdinalIgnoreCase);
    }

    public static bool ExHasAllEquals(this string text, params string[] textArgs)
    {
        for (int index = 0; index < textArgs.Length; index++)
            if (ExEquals(text, textArgs[index]) == false) return false;
        return true;
    }

    public static bool ExHasEquals(this string text, params string[] textArgs)
    {
        for (int index = 0; index < textArgs.Length; index++)
            if (ExEquals(text, textArgs[index])) return true;
        return false;
    }

    public static bool ExHasNoEquals(this string text, params string[] textArgs)
    {
        return ExHasEquals(text, textArgs) == false;
    }

    public static bool ExHasNotAllEquals(this string text, params string[] textArgs)
    {
        for (int index = 0; index < textArgs.Length; index++)
            if (ExEquals(text, textArgs[index])) return false;
        return true;
    }

    /// <summary>
    /// Reports the zero-based index of the first occurrence of the specified string
    /// in the current System.String object using StringComparison.InvariantCultureIgnoreCase.
    /// A parameter specifies the type of search to use for the specified string.
    /// </summary>
    /// <param name="fullText">
    /// The string to search inside.
    /// </param>
    /// <param name="value">
    /// The string to seek.
    /// </param>
    /// <returns>
    /// The index position of the value parameter if that string is found, or -1 if it
    /// is not. If value is System.String.Empty, the return value is 0.
    /// </returns>
    /// <exception cref="ArgumentNullException">
    /// fullText or value is null.
    /// </exception>
    public static int ExIndexOf(this string fullText, string value)
    {
        return fullText.IndexOf(value, StringComparison.OrdinalIgnoreCase);
    }

    public static bool ExNotEquals(this string text, string textToCompare)
    {
        return ExEquals(text, textToCompare) == false;
    }

    #endregion Public Methods
}

这是干净和简单的。

Regex.IsMatch(file, fileNamestr, RegexOptions.IgnoreCase)

这里的技巧是查找字符串,忽略大小写,但保持完全相同(大小写相同)。

 var s="Factory Reset";
 var txt="reset";
 int first = s.IndexOf(txt, StringComparison.InvariantCultureIgnoreCase) + txt.Length;
 var subString = s.Substring(first - txt.Length, txt.Length);

输出为“重置”