C#中不区分大小写的比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10797309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:17:15  来源:igfitidea点击:

Case Insensitive comparison in C#

c#stringcomparison

提问by PhOeNiX

I am comparing two strings using following code

我正在使用以下代码比较两个字符串

string1.Contains(string2)

string1.Contains(string2)

but i am not getting results for case insensitive search. Moreover I cant use String.Compare coz i dont want to match the whole name as the name is very big. My need is to have case insensitive search and the search text can be of any length which the String1 contains.

但我没有得到不区分大小写搜索的结果。此外,我不能使用 String.Compare 因为我不想匹配整个名称,因为名称非常大。我需要的是不区分大小写的搜索,搜索文本可以是 String1 包含的任何长度。

Eg Term**************is the name. I enter "erm" in textbox den i get the result. but when i enter "term" i dont get any result. Can anyone help me :)

例如 Term* *** *** *** *** *是名称。我在文本框中输入“erm”我得到了结果。但是当我输入“term”时,我没有得到任何结果。谁能帮我 :)

采纳答案by paul

string.Equals("this will return true", "ThIs WiLL ReTurN TRue", StringComparison.CurrentCultureIgnoreCase)

string.Equals("this will return true", "ThIs WiLL ReTurN TRue", StringComparison.CurrentCultureIgnoreCase)

or, for contains

或者,对于包含

if (string1.IndexOf(string2, StringComparison.CurrentCultureIgnoreCase) >= 0)

if (string1.IndexOf(string2, StringComparison.CurrentCultureIgnoreCase) >= 0)

回答by Habib

Convert both strings to a same case, either upperor lower.

将两个字符串转换为相同的大小写大写小写

string1.ToUpper().Contains(string2.ToUpper());

回答by Jakub Konecki

string1.ToUpperInvariant().Contains(string2.ToUpperInvariant());

回答by Paolo Tedesco

You can either convert both strings to uppercase, or use regular expressions:

您可以将两个字符串都转换为大写,也可以使用正则表达式:

using System.Text.RegularExpressions;

class Program {
    static void Main(string[] args) {
        string string1 = "TermSomething";
        string string2 = "term";
        bool test1 = string1.ToUpperInvariant().Contains(string2.ToUpperInvariant());
        bool test2 = Regex.IsMatch(string1, Regex.Escape(string2), RegexOptions.IgnoreCase);
    }
}

Note that if you use regular expressions you should escape the search string, so that special regex characters are interpreted literally.

请注意,如果您使用正则表达式,您应该对搜索字符串进行转义,以便按字面解释特殊的正则表达式字符。

回答by gethomast

Why not this:

为什么不是这个:

if (string1.IndexOf(string2, StringComparison.OrdinalIgnoreCase) >= 0)
{  
}

回答by Steve

I prefer an extension method like this.

我更喜欢这样的扩展方法。

public static class StringExtensions
{
    public static bool Contains(this string source, string value, StringComparison compareMode)
    {
        if (string.IsNullOrEmpty(source))
            return false;

        return source.IndexOf(value, compareMode) >= 0;
    }
}

Notice that in this way you could avoid the costly transformation in upper or lower case.

请注意,通过这种方式,您可以避免大写或小写的昂贵转换。

You could call the extension using this syntax

您可以使用此语法调用扩展

 bool result = "This is a try".Contains("TRY", StringComparison.InvariantCultureIgnoreCase);
 Console.WriteLine(result);

Please note: the above extension (as true for every extension method) should be defined inside a non-nested, non-generic static class See MSDN Ref

请注意:上面的扩展(对于每个扩展方法都是如此)应该定义在一个非嵌套的、非通用的静态类中,参见 MSDN Ref

回答by Vijay

Regex.IsMatch(string1,string2,RegexOptions.IgnoreCase);

This returns boolean value.....

这将返回布尔值.....