Counting charater occurances efficiently

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

What is the most efficient way to count occurances of charaters in a string?

IE. Searching for ',' in a comma del file.
 
Peter said:
What is the most efficient way to count occurances of charaters in
a string?

IE. Searching for ',' in a comma del file.

\\\
Dim n As Integer = CountOccurances(..., ","c)
..
..
..
Private Function CountOccurances( _
ByVal Text As String, _
ByVal Character As Char _
) As Integer
Dim Count As Integer
For Each c As Char In Text
If c = Character Then
Count += 1
End If
Next c
Return Count
End Function
///
 
Peter,

This was the best result in a test I did last year when it is about a char.
This code was supplied by Jay B. Harlow

Public Function test5(ByVal input As String, ByVal _
delimiter As Char) As Integer
Dim count, index As Integer
index = input.IndexOf(delimiter)
Do Until index < 0
count += 1
index = input.IndexOf(delimiter, index + 1)
Loop
Return count

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top