2

Hi I'm trying to get the number of occurrences of a character out of a txtbox. Still haven't found the answer... For example: I give in a sentence... "Hello there!." and in a listbox there must be...

H - 2 times e - 3 times ....

this is my code...

  For i = 0 To txtSent.Text.Length - 1

        If (Char.IsLetter(txtSent.Text(i))) Then
            Dim str = Len(txtSent.Text) - Len(Replace(txtSen.Text, txtSen.Text(i), ""))

            lstOutput.Items.Add(txtZin.Text(i) & " occurs " & str & " time(s)")
        End If

    Next´

But i need it to be "m - 5" instead of repeating all the characters of "m"

Can you help me?

4

1 回答 1

1

看看这篇文章。做你所追求的。http://msdn.microsoft.com/en-us/library/bb397940.aspx

这是 vb.net 中的一种方法,也应该对您有所帮助。

    公共函数 GetNumSubstringOccurrences(ByVal text As String, ByVal search As String) As Integer
        Dim num As Integer = 0
        Dim pos As Integer = 0
        If Not String.IsNullOrEmpty(text) AndAlso Not String.IsNullOrEmpty(search) Then
            而 text.IndexOf(search.ToLower(), pos) > -1
                数字 += 1
                pos = text.ToLower().IndexOf(search.ToLower(), pos) + search.Length + 1
            结束时
        万一
        返回号码
    结束功能

要循环字母表,请执行以下操作

Dim s As String = "ssssddfffccckkkllkeeiol"
        对于 "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray() 中的每个 c
            Console.WriteLine(GetNumSubstringOccurrences(s, c))
        下一个
于 2013-06-10T18:22:53.140 回答