不区分大小写'包含(字符串)'

问题描述 投票:2684回答:24

有没有办法让以下返回true?

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

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

UPDATE 这个问题是古老的,从那时起我就意识到,如果你想完全研究它,我会要求一个简单的答案来解决一个非常庞大而困难的话题。 对于大多数情况,在单语言,英语代码库this答案就足够了。我怀疑是因为大多数人来到这个类别这是最受欢迎的答案。 然而,This答案提出了我们无法比较文本不区分大小写的固有问题,直到我们知道两种文本是相同的文化并且我们知道文化是什么。这可能是一个不太受欢迎的答案,但我认为它更正确,这就是为什么我这样标记它。

c# string contains case-insensitive
24个回答
1289
投票

测试字符串paragraph是否包含字符串word(感谢@QuarterMeister)

culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0

其中cultureCultureInfo的实例,用于描述文本所用的语言。

这种解决方案对于不区分大小写的定义是透明的,这种定义与语言有关。例如,英语使用字符Ii作为第九个字母的大写和小写版本,而土耳其语使用这些字符作为其29个字母长字母表的eleventh and twelfth letters。土耳其大写版本的'i'是不熟悉的角色'İ'。

因此字符串tinTIN在英语中是相同的单词,但在土耳其语中是不同的单词。据我所知,一个是“精神”,另一个是拟声词。 (土耳其人,请纠正我,如果我错了,或建议一个更好的例子)

总而言之,如果你知道文本是什么语言,你只能回答“这两个字符串是否相同但在不同情况下”的问题。如果你不知道,你将不得不采取行动。考虑到英语在软件方面的霸权,你应该诉诸于CultureInfo.InvariantCulture,因为它会以熟悉的方式出错。


26
投票

OrdinalIgnoreCase,CurrentCultureIgnoreCase还是InvariantCultureIgnoreCase?

由于缺少这个,这里有一些关于何时使用哪一个的建议:

  • 使用StringComparison.OrdinalIgnoreCase进行比较,作为与文化无关的字符串匹配的安全默认值。
  • 使用StringComparison.OrdinalIgnoreCase比较来提高速度。
  • 在向用户显示输出时使用StringComparison.CurrentCulture-based字符串操作。
  • 切换当前使用基于不变文化的字符串操作,以便在比较时使用非语言StringComparison.OrdinalStringComparison.OrdinalIgnoreCase 语言上无关紧要(例如,象征性的)。
  • 在对字符串进行标准化以进行比较时,请使用ToUpperInvariant而不是ToLowerInvariant

注意事项

  • 对未明确或隐式指定字符串比较机制的字符串操作使用重载。
  • 使用基于StringComparison.InvariantCulture的字符串 大多数情况下的操作;少数例外之一 坚持语言上有意义但与文化无关的数据。

根据这些规则,您应该使用:

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

而[YourDecision]取决于上面的建议。

来源链接:http://msdn.microsoft.com/en-us/library/ms973919.aspx


13
投票

这些是最简单的解决方案。

  1. 按索引 string title = "STRING"; if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1) { // contains }
  2. 通过改变案例 string title = "STRING"; bool contains = title.ToLower().Contains("string")
  3. 通过Regex Regex.IsMatch(title, "string", RegexOptions.IgnoreCase);

11
投票

我知道这不是C#,但在框架(VB.NET)中已经存在这样的功能

Dim str As String = "UPPERlower"
Dim b As Boolean = InStr(str, "UpperLower")

C#变种:

string myString = "Hello World";
bool contains = Microsoft.VisualBasic.Strings.InStr(myString, "world");

11
投票

如果您对国际化存在担忧(或者您可以重新实现它),那么来自VisualBasic程序集的InStr方法是最好的。在它看来dotNeetPeek显示它不仅占大写字母和小写字母,而且还包括假名类型和全角度与半宽字符(主要与亚洲语言相关,尽管罗马字母也有全宽版本) )。我正在跳过一些细节,但查看私有方法InternalInStrText

private static int InternalInStrText(int lStartPos, string sSrc, string sFind)
{
  int num = sSrc == null ? 0 : sSrc.Length;
  if (lStartPos > num || num == 0)
    return -1;
  if (sFind == null || sFind.Length == 0)
    return lStartPos;
  else
    return Utils.GetCultureInfo().CompareInfo.IndexOf(sSrc, sFind, lStartPos, CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth);
}

11
投票

像这样:

string s="AbcdEf";
if(s.ToLower().Contains("def"))
{
    Console.WriteLine("yes");
}

10
投票

用这个:

string.Compare("string", "STRING", new System.Globalization.CultureInfo("en-US"), System.Globalization.CompareOptions.IgnoreCase);

7
投票

使用RegEx是一种直接的方法:

Regex.IsMatch(title, "string", RegexOptions.IgnoreCase);

5
投票

这与此处的其他示例非常相似,但我决定将枚举简化为bool,primary,因为通常不需要其他替代方法。这是我的例子:

public static class StringExtensions
{
    public static bool Contains(this string source, string toCheck, bool bCaseInsensitive )
    {
        return source.IndexOf(toCheck, bCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) >= 0;
    }
}

用法如下:

if( "main String substring".Contains("SUBSTRING", true) )
....

2
投票
if ("strcmpstring1".IndexOf(Convert.ToString("strcmpstring2"), StringComparison.CurrentCultureIgnoreCase) >= 0){return true;}else{return false;}

2
投票

你可以使用string.indexof ()功能。这将不区分大小写


2585
投票

你可以使用String.IndexOf Method并传递StringComparison.OrdinalIgnoreCase作为要使用的搜索类型:

string title = "STRING";
bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;

更好的是为字符串定义一个新的扩展方法:

public static class StringExtensions
{
    public static bool Contains(this string source, string toCheck, StringComparison comp)
    {
        return source?.IndexOf(toCheck, comp) >= 0;
    }
}

请注意,自C#6.0(VS 2015)以来,null propagation ?.可用,旧版本使用

if (source == null) return false;
return source.IndexOf(toCheck, comp) >= 0;

用法:

string title = "STRING";
bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);

2
投票

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

 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);

输出是“重置”


2
投票

只是为了在这里建立答案,你可以创建一个字符串扩展方法,使这个用户更友好:

    public static bool ContainsIgnoreCase(this string paragraph, string word)
    {
        return new CultureInfo("en-US").CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0;
    }

0
投票
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
}

0
投票

如果你想检查你传递的字符串是否在字符串中,那么有一个简单的方法。

string yourStringForCheck= "abc";
string stringInWhichWeCheck= "Test abc abc";

bool isContaines = stringInWhichWeCheck.ToLower().IndexOf(yourStringForCheck.ToLower()) > -1;

This boolean value will return if string contains or not

-4
投票

新手的简单方法:

title.ToLower().Contains("string");//of course "string" is lowercase.

219
投票

你可以像这样使用IndexOf()

string title = "STRING";

if (title.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
{
    // The string exists in the original
}

由于0(零)可以是索引,因此请检查-1。

MSDN

如果找到该字符串,则从零开始的索引位置值,如果不是,则返回-1。如果value为String.Empty,则返回值为0。


136
投票

使用Regex的替代解决方案:

bool contains = Regex.IsMatch("StRiNG to search", Regex.Escape("string"), RegexOptions.IgnoreCase);

76
投票

您可以先将字符串向上或向下放大。

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

哎呀,刚看到最后一点。不区分大小写的比较*probably*无论如何都会做同样的事情,如果性能不是问题,我认为创建大写副本并比较它们没有问题。我曾经发誓我曾经看过一次不区分大小写的比较...


51
投票

答案的一个问题是,如果字符串为null,它将抛出异常。您可以将其添加为支票,因此不会:

public static bool Contains(this string source, string toCheck, StringComparison comp)
{
    if (string.IsNullOrEmpty(toCheck) || string.IsNullOrEmpty(source))
        return true;

    return source.IndexOf(toCheck, comp) >= 0;
} 

36
投票

这很简洁。

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

35
投票

StringExtension类是前进的方法,我结合上面的几个帖子给出了一个完整的代码示例:

public static class StringExtensions
{
    /// <summary>
    /// Allows case insensitive checks
    /// </summary>
    public static bool Contains(this string source, string toCheck, StringComparison comp)
    {
        return source.IndexOf(toCheck, comp) >= 0;
    }
}

28
投票

.NET Core 2.0+ only (as of now)

从2.0版开始,.NET Core就有了一对处理这个问题的方法:

  • String.Contains(Char,StringComparison)
  • String.Contains(String,StringComparison)

例:

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

随着时间的推移,他们可能会进入.NET标准,并从那里进入基类库的所有其他实现。

© www.soinside.com 2019 - 2024. All rights reserved.