VB.NET 'Like'运算符中可能存在错误?



为什么下面的计算结果为True

Dim result = "b" Like "*a*b"

谢谢。

编辑:
为了概括这一点,以下返回True

"String1" Like "*AnyText1*AnyText2*AnyText???******????*String1"

VBA 工作正常,返回False .
PowerShell 工作正常,返回False

PS C:UsersXXX> "b" -Like "*a*b"
False

编辑2:
指向错误报告的链接:
https://connect.microsoft.com/VisualStudio/feedback/details/748415/a-bug-in-net-like-operator

为了好玩,我决定打开 ilspy 来调试这个:-)

在这种方法中;

    private static void MatchAsterisk(string Source, int SourceLength, int SourceIndex, LigatureInfo[] SourceLigatureInfo, string Pattern, int PatternLength, int PatternIndex, LigatureInfo[] PattternLigatureInfo, ref bool Mismatch, ref bool PatternError, CompareInfo Comparer, CompareOptions Options)

对于这种情况

                if (SourceLength <= 0)
                {
                    return;
                }

通过将其更改为

                if (SourceLength < 0)
                {
                    return;
                }

似乎使它按预期工作

我只做了几个小测试,没什么大不了的

问题是; 它只查看最后一个星号,当它有效时,就在那里停止

我的小更改确保检查前一个,或者之前的任何内容

用我的修复

 Dim result = "b" Like "*a*b"
 now return false
 "String1" Like "*AnyText1*AnyText2*AnyText???******????*String1"
 now return false

但当需要返回 true 时返回 true

最新更新