我需要一个用字符串索引的数组,如下所示:
Dim Array ("A") as integer' <-- I need an array like this
Dim StringArray() as string = {"A","B","C","A","A","B","D"}
For each Letter in StringArray
Array(Letter) += 1
next
我想要但没有工作的结果:
Array(A) = 3
Array(B) = 2
Array(C) = 1
Array(D) = 1
我,也试过List's,不工作:
Dim Array As New List(Of Object)
Dim StringArray() as string = {"A","B","C","A","A","B","D"}
For each Letter in StringArray
Array(Letter) += 1
next
有没有办法在VB .Net中以这种方式计算字符串?
答案 0 :(得分:4)
您可以使用Dictionary(Of TKey, TValue)
字母作为键,值将存储您的总数:
Dim dict = New Dictionary(Of Char, Integer)
dict.Add("A"c, 0)
dict.Add("B"c, 0)
dict.Add("C"c, 0)
dict.Add("D"c, 0)
Dim stringArray() As Char = {"A"c, "B"c, "C"c, "A"c, "A"c, "B"c, "D"c}
For Each letter In stringArray
dict.Item(letter) += 1
Next