在运行时与 VB.NET LIKE 运算符进行区分大小写(不区分大小写)的比较(不带选项比较)

发布于 2024-11-17 08:40:24 字数 159 浏览 5 评论 0原文

无论如何,在运行时是否可以在 VB.NET 中使用 LIKE 运算符区分大小写或不区分大小写?例如,使用标志进行区分大小写或不区分大小写的比较。

显然,这可以通过简单地将它们转换为小写并强制应用程序使用 Option Compare Binary 来完成,但也许有更好的方法来做到这一点?

Is there anyway to use LIKE operator in VB.NET as case sensitive or insensitive during runtime? For example use a flag to do case sensitive or insensitive comparisons.

Obviously this can be done by simple converting them into lower case and forcing application to Option Compare Binary but maybe there is a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿哥们儿 2024-11-24 08:40:24

我不这么认为。但是,如果不区分大小写很重要,您可能无论如何都不应该使用 Like 运算符 - 相反,请使用正则表达式。

Dim re As New System.Text.RegularExpressions.Regex("^.+ough$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

re.IsMatch("rough") ' True
re.IsMatch("tough") ' True
re.IsMatch("rOUGH") ' True
re.IsMatch("ough")  ' False

有很多东西需要学习,但基本上 . 相当于 ?.* 相当于 *,并且\d 相当于#。您也必须将其包装在 ^$ 中以实现等效。正则表达式功能更强大,可以满足您的需要。

如果您打算经常使用它们,您可能应该添加 Imports System.Text.RegularExpressions。它们也可以被编译和重用以提高效率。

I don't think so. However, you should probably not use the Like operator anyways if case-insensitivity is important - instead, use regular expressions.

Dim re As New System.Text.RegularExpressions.Regex("^.+ough$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

re.IsMatch("rough") ' True
re.IsMatch("tough") ' True
re.IsMatch("rOUGH") ' True
re.IsMatch("ough")  ' False

There's a lot to learn, but basically . is equivalent to ?, .* is equivalent to *, and \d is equivalent to #. You have to wrap it in ^ and $ for equivalency, too. Regular expressions are much more powerful and will do what you need.

You should probably add Imports System.Text.RegularExpressions if you plan to use them a lot. They can be compiled and reused for efficiency, too.

沉溺在你眼里的海 2024-11-24 08:40:24

您可以提供自定义类来确保进行不区分大小写的比较,即使默认设置为“比较二进制”(区分大小写)也是如此。您可以在代码文件中指定Option Compare

Option Compare Text

Public Class CaseInsensitiveLikeOperator
    Public Shared Function IsLike(str As String, pattern As String) As Boolean
        Return str Like pattern
    End Function
End Class

现在可以了:

Dim isSame = CaseInsensitiveLikeOperator.IsLike("foo", "Fo?") ' True

如果您的默认值是Option Compare Text,为了安全起见,您可以提供两个类。

也许最好的选择是学习正则表达式;-)

You could provide a custom class to ensure that you get case case-insensitive comparison even if the default settings is Compare Binary(case sensitive). You can specify the Option Compare in a code-file:

Option Compare Text

Public Class CaseInsensitiveLikeOperator
    Public Shared Function IsLike(str As String, pattern As String) As Boolean
        Return str Like pattern
    End Function
End Class

Now this works:

Dim isSame = CaseInsensitiveLikeOperator.IsLike("foo", "Fo?") ' True

If your default is Option Compare Text you could provide two classes to be on the safe side.

Maybe the best option is to learn regex ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文