Visual Basic:如何计算句子中每个字母的出现次数? (计算“ a”或“ f”的数量)

时间:2019-05-04 07:57:44

标签: vb.net

我正在编写一些代码来识别文本框中的字母数量。

“每个人都很忙,所以我一个人去看电影。”

该代码将识别出存在7个“ e”,并将对整个字母执行此操作。如果混合大小写的字母应该一起计算(尽管其中一个大写,它仍然会计算7个'e')。我该怎么办?

2 个答案:

答案 0 :(得分:2)

这是一个控制台应用程序,展示了如何完成它。 请注意,它不会区分大小写,也不会区分非字母符号。如果需要,可以将这些过滤掉。

Sub Main()

        ' declare variables and objects to use 
        Dim charCollection As Dictionary(Of Char, Int32) = New Dictionary(Of Char, Int32)()
        Dim paragraph As String = "Everyone was busy, so I went to the movie alone."

        ' iterate each character in paragraph and use each one as key in the dictionary list
        ' if letter exist increment value for that key else add new key
        For Each chr As Char In paragraph
            If charCollection.ContainsKey(chr) Then
                charCollection.Item(chr) = charCollection.Item(chr) + 1
            Else
                charCollection.Add(chr, 1)
            End If
        Next

        ' display interval of each character
        For Each chr As Char In charCollection.Keys
            Console.WriteLine($"the count of character {chr} is : {charCollection.Item(chr)}")
        Next
        Console.WriteLine($"Total of {charCollection.Keys.Count} different characters")

    End Sub

输出为:

the count of character E is : 1
the count of character v is : 2
the count of character e is : 6
the count of character r is : 1
the count of character y is : 2
the count of character o is : 5
the count of character n is : 3
the count of character   is : 9
the count of character w is : 2
the count of character a is : 2
the count of character s is : 3
the count of character b is : 1
the count of character u is : 1
the count of character , is : 1
the count of character I is : 1
the count of character t is : 3
the count of character h is : 1
the count of character m is : 1
the count of character i is : 1
the count of character l is : 1
the count of character . is : 1
Total of 21 different characters

编辑:解释小写字母和非数字/字母的另一个版本 编辑2:添加:段落= paragraph.ToLower(),已删除:lcChr = Char.ToLower(chr)

Sub Main()

' declare variables and objects to use 
Dim charCollection As Dictionary(Of Char, Int32) = New Dictionary(Of Char, Int32)()
Dim paragraph As String = "Everyone was busy, so I went to the movie alone."
Dim lcChr As Char

paragraph = paragraph.ToLower() 

' iterate each character in paragraph and use each one as key in the dictionary list
' if letter exist increment value for that key else add new key
For Each chr As Char In paragraph        
    If Char.IsLetterOrDigit(chr) Then
        If charCollection.ContainsKey(chr) Then
            charCollection.Item(chr) = charCollection.Item(chr) + 1
        Else
            charCollection.Add(chr, 1)
        End If
    End If
Next

' display interval of each character
For Each chr As Char In charCollection.Keys
    Console.WriteLine($"the count of character {chr} is : {charCollection.Item(chr)}")
Next
Console.WriteLine($"Total of {charCollection.Keys.Count} different characters")

End Sub

答案 1 :(得分:0)

您也可以使用LINQ来缩短代码

Public Sub Main()
        Dim myText As String = "Everyone was busy, so I went to the movie alone.".ToLower()

        'Get each character including space
        For Each c As Char In myText.Distinct
            'Count the occurrence of characters and write to the console
            Console.WriteLine(c & " occurs " & myText.Count(Function(d As Char) d = c) & " times")

        Next

End Sub