VB.NET 选项比较文本在 .NET 3.5 中无法正常工作

发布于 2025-01-16 00:28:32 字数 1824 浏览 4 评论 0原文

我正在使用 VB.NET 开发旧版 Windows 窗体,最近发现了一个令人困惑的行为。

有一个项目 A 面向 .NET 2.0,并在代码文件中设置了选项比较文本。项目 A 包含使用 String.InStr 方法的函数 CharacterFilter。 CharacterFilter 将从给定字符串中删除特殊字符。

项目 B 引用项目 A。项目 B 面向 .NET 3.5,并且还在其代码文件中设置了 Option Compare Text。

给定输入字符串“123”并且要删除的特殊字符是“²³”,令人惊讶的是,CharacterFilter 函数不会从给定输入字符串中删除“23”。但是,如果项目 B 以 .NET 4.0 为目标,则将从给定的输入字符串中删除“23”。

从 Microsoft 文档来看,无论 .net 框架版本如何,设置选项比较文本都应删除“23”。 https:// learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/option-compare-statement

有人可以帮忙指出报告此行为的 Microsoft 文档吗?非常感谢。

--Project A targets .NET 2.0
Option Compare Text
Public Class Backtool
    Function CharFilter(ByVal Text As String, ByVal Filter_Renamed As String) As String
        Dim i As Integer
        Dim vF As String
        Dim vPos As Integer

            For i = Len(Filter_Renamed) To 1 Step -1
                vF = Mid(Filter_Renamed, i, 1)
                vPos = InStr(Text, vF)
                Do While vPos > 0
                    Text = Mid(Text, 1, vPos - 1) & Mid(Text, vPos + 1)
                    vPos = InStr(Text, vF)
                Loop
            Next i
        
        CharFilter = Text
    End Function
End Class
--Project B that references project A
Option Compare Text
Module Module1
    Public BT As Backtool
    Sub Main()
        BT = New Backtool()
        Dim text = "123"
        Dim filterStr = "²³"
        Console.WriteLine("Result string is: " + BT.CharFilter(text, filterStr, False))
        --Returns "123" if targets .NET Framework 3.5
        --Returns "1" if targets .NET Framework 4.0
    End Sub

End Module

I am working on a legacy Windows Form with VB.NET and I have recently discovered a confusing behavior.

There is project A that targets .NET 2.0 with Option Compare Text set at code file. Project A contains function CharacterFilter that uses method String.InStr. CharacterFilter will remove special characters from given string.

Project B references to project A. Project B targets .NET 3.5 and also set Option Compare Text at its code file.

Given input string as "123" and special characters to be removed are "²³", function CharacterFilter surprisingly does not remove "23" from given input string. However, if Project B is targetted .NET 4.0, "23" will be removed from given input string.

From Microsoft docs, setting Option Compare Text should remove "23" no matter what .net framework versions.
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/option-compare-statement

Could someone help to point a Microsoft document reporting this behavior. Many thanks.

--Project A targets .NET 2.0
Option Compare Text
Public Class Backtool
    Function CharFilter(ByVal Text As String, ByVal Filter_Renamed As String) As String
        Dim i As Integer
        Dim vF As String
        Dim vPos As Integer

            For i = Len(Filter_Renamed) To 1 Step -1
                vF = Mid(Filter_Renamed, i, 1)
                vPos = InStr(Text, vF)
                Do While vPos > 0
                    Text = Mid(Text, 1, vPos - 1) & Mid(Text, vPos + 1)
                    vPos = InStr(Text, vF)
                Loop
            Next i
        
        CharFilter = Text
    End Function
End Class
--Project B that references project A
Option Compare Text
Module Module1
    Public BT As Backtool
    Sub Main()
        BT = New Backtool()
        Dim text = "123"
        Dim filterStr = "²³"
        Console.WriteLine("Result string is: " + BT.CharFilter(text, filterStr, False))
        --Returns "123" if targets .NET Framework 3.5
        --Returns "1" if targets .NET Framework 4.0
    End Sub

End Module

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2025-01-23 00:28:32

我已经在 Visual Studio 2010 中重现了您的项目,并且出现了同样的奇怪错误。我读过微软的文档,但没有找到原因。

但是,我找到了解决方法...尝试在

Option Compare Text
Imports System.Text

Public Class Backtool
    Function CharFilter(ByVal Text As String, ByVal Filter_Renamed As String) As String
        Dim i As Integer
        Dim vF As String
        Dim vPos As Integer

        'Try this
        Filter_Renamed = Filter_Renamed.Normalize(NormalizationForm.FormKC)
        '

        For i = Len(Filter_Renamed) To 1 Step -1
            vF = Mid(Filter_Renamed, i, 1)
            vPos = InStr(Text, vF)
            Do While vPos > 0
                Text = Mid(Text, 1, vPos - 1) & Mid(Text, vPos + 1)
                vPos = InStr(Text, vF)
            Loop
        Next i
        CharFilter = Text
    End Function
End Class

Microsoft 规范化表单文档下面的

ProjectB 中使用 System.Text.NormalizationForm https://learn.microsoft.com/en-us/dotnet/api/system.text.normalizationform?view=netframework-2.0

我希望它有用。

I have reproduce your projects in Visual Studio 2010 and I have the same stranger error. I have read Microsoft documentation but I can't find the reason.

But, I'v found a work around... try to use System.Text.NormalizationForm inside the ProjectB

Option Compare Text
Imports System.Text

Public Class Backtool
    Function CharFilter(ByVal Text As String, ByVal Filter_Renamed As String) As String
        Dim i As Integer
        Dim vF As String
        Dim vPos As Integer

        'Try this
        Filter_Renamed = Filter_Renamed.Normalize(NormalizationForm.FormKC)
        '

        For i = Len(Filter_Renamed) To 1 Step -1
            vF = Mid(Filter_Renamed, i, 1)
            vPos = InStr(Text, vF)
            Do While vPos > 0
                Text = Mid(Text, 1, vPos - 1) & Mid(Text, vPos + 1)
                vPos = InStr(Text, vF)
            Loop
        Next i
        CharFilter = Text
    End Function
End Class

Below Microsoft normalizationForm documentation

https://learn.microsoft.com/en-us/dotnet/api/system.text.normalizationform?view=netframework-2.0

I hope it is useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文