C# 如何使文本不区分大小写?

C# 如何使文本不区分大小写?,c#,C#,请建议我一种使文本不区分大小写的方法。无论用户使用何种情况,都需要比较文本框输入 例如: 如果textBox1输入为Name/Name/Name,则标签应显示相应的值。只需使用或 如果需要使用不变区域性的大小写规则,请使用大写 if (textBox1.Text.ToUpperInvariant() == "NAME") 您可以实例化比较器并在整个代码中重用它 此不敏感类提供了一组完整的不敏感比较方法: public static class Insensitive { private

请建议我一种使文本不区分大小写的方法。无论用户使用何种情况,都需要比较文本框输入

例如:

如果textBox1输入为Name/Name/Name,则标签应显示相应的值。

只需使用或

如果需要使用不变区域性的大小写规则,请使用大写

if (textBox1.Text.ToUpperInvariant() == "NAME")

您可以实例化比较器并在整个代码中重用它

此不敏感类提供了一组完整的不敏感比较方法:

public static class Insensitive
{
    private static IComparer m_Comparer = CaseInsensitiveComparer.Default;

    public static IComparer Comparer
    {
        get{ return m_Comparer; }
    }

    public static int Compare( string a, string b )
    {
        return m_Comparer.Compare( a, b );
    }

    public static bool Equals( string a, string b )
    {
        if ( a == null && b == null )
            return true;
        else if ( a == null || b == null || a.Length != b.Length )
            return false;

        return ( m_Comparer.Compare( a, b ) == 0 );
    }

    public static bool StartsWith( string a, string b )
    {
        if ( a == null || b == null || a.Length < b.Length )
            return false;

        return ( m_Comparer.Compare( a.Substring( 0, b.Length ), b ) == 0 );
    }

    public static bool EndsWith( string a, string b )
    {
        if ( a == null || b == null || a.Length < b.Length )
            return false;

        return ( m_Comparer.Compare( a.Substring( a.Length - b.Length ), b ) == 0 );
    }

    public static bool Contains( string a, string b )
    {
        if ( a == null || b == null || a.Length < b.Length )
            return false;

        a = a.ToLower();
        b = b.ToLower();

        return ( a.IndexOf( b ) >= 0 );
    }
}

来源:

我想你在找

您也可以尝试查看


比较字符串时,您确实希望使用.Equals方法

第二个参数允许您指定StringComparison。在本例中,它告诉它忽略该案例。

请尝试以下操作:

希望这有帮助

试试这个:

if(textBox1.Text.Equals("Name",StringComparision.InvariantCultureIgnoreCase))
{
  label1.Content = "This is" + textBox1.Text;
}

试试这样的

if (textBox1.Text.ToLowerInvariant() == "Name".ToLowerInvariant())
     label1.Content = "This is" + textBox1.Text;


使用String.ToUpper不要使用String.ToUpper。请使用适当的Equals方法。@DasKrümelmonster为什么?原因是什么?首先,你想表达平等。没有什么比a.Equals更好的了。==可能会,但在将一个参数转换为大写时不会。第二,你只写了,一开始是图珀。这忽视了文化,可能会导致以后的语言问题。如果我的文化将大写字母m定义为ᴍ 感谢大家的及时回复,但我似乎遗漏了一些东西,因为当我尝试执行您的指令时,它显示了一个错误:“StringComparison”名称在当前上下文中不存在。敬请指教!解决项目未显示在菜单中。我已申报使用该系统;声明和系统文本;和System.Text.RegularExpressions@用户3172531…转到对象浏览器,在mscorlib中,检查是否有StringComparer类。您的建议有效!当我昨天尝试这个的时候,其他的东西可能会出问题。谢谢,祝你今天愉快!谢谢按建议更改了我的用户名!感谢大家的及时回复,但我似乎遗漏了一些东西,因为当我尝试实现您的指令时,它显示了一个错误:“StringComparison”名称在当前上下文中不存在。敬请指教!确保你有一个使用系统;在你的文件顶部的声明,虽然你为什么不被怀疑。是的,我尝试了你的建议,但它仍然返回相同的结果error@user3172531:-虽然我不知道为什么它不适合你,因为它在我这边看起来不错。无论如何,您也可以这样尝试:-string.CompareInfo.IndexOftextBox1.Text、Name、CompareOptions.ignoreCase这也不起作用!无论如何,非常感谢你的努力!嗨,我按照建议试过了,但没有达到预期的效果。哪种效果更好p请选择内联内容而不是仅链接的答案。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面更改,仅链接的答案可能会无效。感谢您的反馈。我有空时会修改答案。
if(string.Equals(textBox1.Text, "Name", StringComparer.CurrentCultureIgnoreCase))
textBox1.Text.Equals("Name", StringComparison.CurrentCultureIgnoreCase);
   if(textBox1.text.Equals("value",StringComparison.InvariantCultureIgnoreCase))
if(textBox1.Text.Equals("Name",StringComparision.InvariantCultureIgnoreCase))
{
  label1.Content = "This is" + textBox1.Text;
}
if (textBox1.Text.ToLowerInvariant() == "Name".ToLowerInvariant())
     label1.Content = "This is" + textBox1.Text;