10

为什么以下评估为True

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

谢谢。

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

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

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

PS C:\Users\XXX> "b" -Like "*a*b"
False

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

4

1 回答 1

6

为了好玩,我决定打开 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

于 2012-06-12T01:43:32.890 回答