vb.net 在 IF 比较中忽略大小写

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

Ignore case in IF comparision

vb.netvisual-studioif-statementcase-sensitiveignore-case

提问by ElektroStudios

Is there an easy way to do this comparision with ignorecase ON?

有没有一种简单的方法可以与 ignorecase ON 进行比较?

If file.Extension = ".Lnk" Then MsgBox(file.Extension)

What i'm trying to do is to get all the ".lnk" or ".LNK" or ".lNk" or " ".Lnk" etc...

我想要做的是获取所有“.lnk”或“.LNK”或“.lNk”或“.Lnk”等...

I know this is possibly with RegEx but... there's an easy way for that example?

我知道这可能与 RegEx 一起使用,但是......那个例子有一个简单的方法吗?

Thankyou for read

谢谢阅读

回答by Steve

Convert the extension to lowercase using ToLower and then compare

使用 ToLower 将扩展名转换为小写,然后比较

If file.Extension.ToLower = ".lnk" Then MsgBox(file.Extension)

And forget Regex for this. It's really overkill and inappropriate

并为此忘记正则表达式。真的是矫枉过正,不合适

回答by D_Bester

Use String.Equalsfor string comparisons. To ignore case use CurrentCultureIgnoreCaseor InvariantCultureIgnoreCase.

使用String.Equals字符串比较。要忽略大小写,请使用CurrentCultureIgnoreCaseInvariantCultureIgnoreCase

If String.Equals("AAA", "aaa", StringComparison.InvariantCultureIgnoreCase) Then

    'more code

End If

MSDN: String.Equals Method (String)

MSDN:String.Equals 方法(字符串)

MSDN: Specifying String Comparisons Explicitly

MSDN:显式指定字符串比较

回答by Hyman Gajanan

use this for ignore case compare

将此用于忽略大小写比较

If String.Compare(file.Extension, ".lnk", True) = 0 Then MsgBox(file.Extension)

change true to false for case sensitive compare

对于区分大小写的比较,将 true 更改为 false

回答by Jacob Hernandez

Module Test

模块测试

Sub Main()
    Dim userString As String = Nothing
    Dim finalString As String = "Jacob"

    Console.WriteLine("Enter username")
    userString = Console.ReadLine()

    If String.Compare(finalString, userString, True) = 0 Then
        Console.WriteLine("Access Granted")
    Else
        Console.WriteLine("Access Denied")
    End If


    Console.ReadLine()

End Sub

End Module

终端模块