【问题标题】:Possible bug in VB.NET 'Like' operator?VB.NET 'Like' 运算符中可能存在的错误?
【发布时间】:2012-06-13 20:47:51
【问题描述】:

为什么下面的计算结果为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

【问题讨论】:

标签: .net vb.net wildcard string-matching sql-like


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 2016-11-22
    • 2014-03-13
    • 2011-07-19
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多