Results 1 to 6 of 6

Thread: [RESOLVED] Count char occurance in str.

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Resolved [RESOLVED] Count char occurance in str.

    Guys,

    How would I go about counting the occurance of a certain character in a given string?

    Looking at the string.compare function but cant quite get my head around it.

    Thanks
    Bob
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Count char occurance in str.

    vb Code:
    1. Dim str As String = "Hi--sdfsdfsdf--"
    2.         MessageBox.Show(str.Split("-").Length)
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Count char occurance in str.

    You could loop through each character and check:

    vb Code:
    1. Dim myString As String = "howmanytimesdoesthelettereoccur"
    2. Dim myCOunt As Integer = 0
    3.  
    4.         For Each ch As Char In myString
    5.             If ch = "e" Then myCOunt += 1
    6.         Next
    7.  
    8.         MessageBox.Show(myCOunt.ToString)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  4. #4
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Count char occurance in str.

    You could use something like this, be aware that it'll be case sensitive.

    vb Code:
    1. Public Function CharCount(ByVal c As Char, ByVal str As String) As Integer
    2.  
    3.         Dim intCount As Integer
    4.  
    5.         For Each character As Char In str
    6.             If character = c Then
    7.                 intCount += 1
    8.             End If
    9.         Next
    10.   Return intCount
    11.     End Function
    If my post helps , please feel free to rate it

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Count char occurance in str.

    This worked for me
    vb Code:
    1. Dim str As String = "Hisdf--sddd---fsdf"
    2.         Dim ncount As Integer = str.Split("-").Length
    3.         If ncount = 0 Then
    4.             Console.WriteLine("0 matches")
    5.         Else
    6.             Console.WriteLine(String.Concat(ncount - 1, " matches found"))
    7.         End If
    Please mark you thread resolved using the Thread Tools as shown

  6. #6

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Count char occurance in str.

    the str.split worked just fine for me.

    Thanks
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width