Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,092 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,268 people online right now. Registration is fast and FREE... Join Now!




5 Ways of counting occurences

 
Reply to this topicStart new topic

> 5 Ways of counting occurences, How time does a peice text occur within another

AdamSpeight2008
Group Icon



post 13 Jul, 2008 - 09:58 PM
Post #1


Using For
vb

Private Function CountOccurs(ByRef original As String, ByRef match As String) As Integer
Dim mCount As Integer = 0
For i As Integer = 0 To original.Length - 1
i = original.IndexOf(match, i)
If i < 0 Then Exit For
mCount += 1
i += 1
Next
Return mCount
End Function


Using While
vb

Private Function CountOccurs(ByRef original As String, ByRef match As String) As Integer
Dim mCount As Integer = 0
Dim i as integer=0
While i>=0
i = original.IndexOf(match, i)
If i < 0 Then Exit For
mCount += 1
i += 1
end wihile
Return mCount
End Function


Using Do
vb

Private Function CountOccurs(ByRef original As String, ByRef match As String) As Integer
Dim mCount As Integer = 0
Dim i As Integer = 0
Do
i = original.IndexOf(match, i)
If i < 0 Then Exit Do
mCount += 1
i += 1
Loop Until i = original.Length
Return mCount
End Function
End Function


Using Recursion
vb

Private Function CountOccurs(ByRef original As String, ByRef match As String, Optional ByRef i As Integer = 0, Optional ByRef mCount As Integer = 0) As Integer
i = original.IndexOf(match, i)
If i < 0 Then Return mCount
mCount += 1
Return CountOccurs(original, match, i, mCount)
End Function


Using Regular Expression
vb

Private Function CountOccurs(ByRef original As String, ByRef match As String) as integer
Return System.Text.RegularExpressions.Regex.Matches(original, match).Count
End Function




Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/21/08 09:01AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month