vb.net 字典键不区分大小写

问题描述 投票:0回答:1

我最近才以迂回的方式遇到了

Option Compare Text
。我们有各种各样的:

soAndSo.Equals("one", StringComparison.CurrentCultureIgnoreCase)

而且这个选项似乎很有用。然而,其中许多比较都在字典的键中,所以我这样做了......

Option Compare Text
'Option Compare Binary
Dim testdict As New Dictionary(Of String, String)
testdict.Add("OneTwo", "onetwo")
testdict.Add("TwoThree", "twothree")
testdict.Add("ThreeFour", "threefour")
If "one" = "One" Then
    Dim y As Integer = 5
End If
If testdict.ContainsKey("onetwo") Then
    Dim y As Integer = 5
End If
If testdict.ContainsValue("OneTwo") Then
    Dim y As Integer = 5
End If
If testdict.ContainsValue("onetwo") Then
    Dim y As Integer = 5
End If

这不起作用,更改选项没有任何效果,字典键或值都不匹配。我对此感到惊讶,因为微软似乎没有使用他们自己的平等测试。

我知道我可以用不同的比较操作制作一本字典,但我想知道我是否只是错过了一些同样简单的开关?谷歌说不,但我不再信任谷歌了。

vb.net dictionary
1个回答
0
投票

导致字典忽略大小写的简短示例,

    Dim dict As New Dictionary(Of String, String)(StringComparer.CurrentCultureIgnoreCase)

    Dim ent As String = "TEST"

    dict.Add(ent, ent)
    If dict.ContainsKey(ent.ToLower) Then
        Debug.WriteLine("duplicate") 'here in debugger
    Else
        dict.Add(ent.ToLower, ent)
    End If
© www.soinside.com 2019 - 2024. All rights reserved.