|
-
Jul 18th, 2007, 10:12 AM
#1
Thread Starter
Fanatic Member
[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
-
Jul 18th, 2007, 10:15 AM
#2
Re: Count char occurance in str.
vb Code:
Dim str As String = "Hi--sdfsdfsdf--"
MessageBox.Show(str.Split("-").Length)
Please mark you thread resolved using the Thread Tools as shown
-
Jul 18th, 2007, 10:19 AM
#3
Re: Count char occurance in str.
You could loop through each character and check:
vb Code:
Dim myString As String = "howmanytimesdoesthelettereoccur"
Dim myCOunt As Integer = 0
For Each ch As Char In myString
If ch = "e" Then myCOunt += 1
Next
MessageBox.Show(myCOunt.ToString)
-
Jul 18th, 2007, 10:23 AM
#4
Hyperactive Member
Re: Count char occurance in str.
You could use something like this, be aware that it'll be case sensitive.
vb Code:
Public Function CharCount(ByVal c As Char, ByVal str As String) As Integer
Dim intCount As Integer
For Each character As Char In str
If character = c Then
intCount += 1
End If
Next
Return intCount
End Function
If my post helps , please feel free to rate it 
-
Jul 18th, 2007, 10:27 AM
#5
Re: Count char occurance in str.
This worked for me
vb Code:
Dim str As String = "Hisdf--sddd---fsdf"
Dim ncount As Integer = str.Split("-").Length
If ncount = 0 Then
Console.WriteLine("0 matches")
Else
Console.WriteLine(String.Concat(ncount - 1, " matches found"))
End If
Please mark you thread resolved using the Thread Tools as shown
-
Jul 18th, 2007, 10:37 AM
#6
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|