Are there any advantages to using the VB.NET Like

2019-07-03 16:04发布

Other than perhaps enhanced readability for very simple patterns, why would someone choose to use the Like operator in VB.NET over regular expressions for string pattern matching? Are there any advantages?

1条回答
等我变得足够好
2楼-- · 2019-07-03 16:42

Probably. If you want to take a look at how Like is implemented, much (all?) of it is in Microsoft.VisualBasic.CompilerServices.LikeOperator, and the rudiments can be seen in #LikeObject and #LikeString. Looking at the documentation, Like obviously uses a pretty strict subset of the full-on regular expression engine, and like just about any Perl-Compatible Regular Expression engine, there's some heavy lifting that may be overkill for simple expressions.

That said, in my opinion, it comes down to style. If you feel that If (myString Like "a?bb") is more readable, idiomatic, and consistent with the rest of your code, go for it. It would appear to me that going either way for anything but the aforementioned reasons is microoptimization theatre, especially since you can compile regexes if you need to.

查看更多
登录 后发表回答