vb.net 不区分大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15606701/
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-09-17 12:58:12  来源:igfitidea点击:

Case insensitive

vb.netvisual-studiocase-insensitive

提问by Brandon

If TextBox2.Text = "a" AndAlso TextBox21.Text = "a" Then
        'MessageBox.Show("A")
        totCorrect = totCorrect + corAns
    ElseIf TextBox2.Text = "b" AndAlso TextBox21.Text = "b" Then
        'MessageBox.Show("B")
        totCorrect = totCorrect + corAns
    ElseIf TextBox2.Text = "c" AndAlso TextBox21.Text = "c" Then
        'MessageBox.Show("C")
        totCorrect = totCorrect + corAns
    ElseIf TextBox2.Text = "d" AndAlso TextBox21.Text = "d" Then
        'MessageBox.Show("D")
        totCorrect = totCorrect + corAns
    Else
        totWrong = totWrong + wrgAns
        Label13.Visible = True
    End If

I am trying to make the letters a,b,c,d that the user enters insensitive. Tried to use the UCase, but it did not work (not sure if I am using it wrong). I am in Visual Studio 2012 and using VB. Any references would be great.

我试图让用户输入的字母 a,b,c,d 不敏感。尝试使用 UCase,但它不起作用(不确定我是否使用错误)。我在 Visual Studio 2012 中使用 VB。任何参考都会很棒。

回答by Iswanto San

You can use String.Comparemethod : String.Compare (String strA, String strB, Boolean ignoreCase)

您可以使用String.Compare方法:String.Compare (String strA, String strB, Boolean ignoreCase)

Pass ignoreCaseargument with truewill perform case insensitive comparison.

传递ignoreCase参数 withtrue将执行不区分大小写的比较。

If String.Compare(TextBox2.Text, "a", true) = 0 AndAlso String.Compare(TextBox21.Text, "a", true) = 0 Then
        'MessageBox.Show("A")
        totCorrect = totCorrect + corAns
    ElseIf String.Compare(TextBox2.Text, "b", true) = 0 AndAlso String.Compare(TextBox21.Text, "b", true) = 0 Then
        'MessageBox.Show("B")
        totCorrect = totCorrect + corAns
    ElseIf String.Compare(TextBox2.Text, "c", true) = 0 AndAlso String.Compare(TextBox21.Text, "c", true) = 0 Then
        'MessageBox.Show("C")
        totCorrect = totCorrect + corAns
    ElseIf String.Compare(TextBox2.Text, "d", true) = 0 AndAlso String.Compare(TextBox21.Text, "d", true) = 0 Then
        'MessageBox.Show("D")
        totCorrect = totCorrect + corAns
    Else
        totWrong = totWrong + wrgAns
        Label13.Visible = True
    End If

Another idea is to uppercase or lowercase the input using ToUpperor ToLower.

另一个想法是使用ToUpperToLower将输入大写或小写。

If TextBox2.Text.ToUpper() = "A" AndAlso TextBox21.Text.ToUpper() = "A" Then
            'MessageBox.Show("A")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text.ToUpper() = "B" AndAlso TextBox21.Text.ToUpper() = "B" Then
            'MessageBox.Show("B")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text.ToUpper() = "C" AndAlso TextBox21.Text.ToUpper() = "C" Then
            'MessageBox.Show("C")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text.ToUpper() = "D" AndAlso TextBox21.Text.ToUpper() = "D" Then
            'MessageBox.Show("D")
            totCorrect = totCorrect + corAns
        Else
            totWrong = totWrong + wrgAns
            Label13.Visible = True
        End If

回答by beppe9000

According to MSDN in VB.NET, you can use the Option Compare Statementby simply adding 1 line of code to your file:

根据 VB.NET 中的 MSDN,您Option Compare Statement只需在文件中添加 1 行代码即可使用:

Option Compare Text

If you add the above line to the beginning of your code you are telling the CLR to switch from default (Option Compare Binary) to the case-insensitive comparison as the new default for the =operator.

如果将上述行添加到代码的开头,则是在告诉 CLR 从默认值 ( Option Compare Binary)切换到不区分大小写的比较,作为=运算符的新默认值。

I don't know if there is any C# alternative.

我不知道是否有任何 C# 替代方案。