在运行时与 VB.NET LIKE 运算符进行区分大小写(不区分大小写)的比较(不带选项比较)
无论如何,在运行时是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不这么认为。但是,如果不区分大小写很重要,您可能无论如何都不应该使用
Like
运算符 - 相反,请使用正则表达式。有很多东西需要学习,但基本上
.
相当于?
,.*
相当于*
,并且\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.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.您可以提供自定义类来确保进行不区分大小写的比较,即使默认设置为“比较二进制”(区分大小写)也是如此。您可以在代码文件中指定
Option Compare
:现在可以了:
如果您的默认值是
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 theOption Compare
in a code-file:Now this works:
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 ;-)