Is there a string function for counting occurances of substrings?

J

Jason Barnett

I thought I had seen a method for returning the number of occurances of a
substring within a string object, but I can't seem to find it now. Does
anyone know of something?

I know I can parse the substrings and count them using a loop, but I'm
looking for a single function or method to simplify the process.
 
Z

Zoury

Hi Jason ! :O)
Does anyone know of something?

I know I can parse the substrings and count them using a loop, but I'm
looking for a single function or method to simplify the process.

You could use the Regex.Split() method and look at the Length of the string
array being returned..

'**
Imports System.Text.RegularExpressions

Public Class MyApplication

Public Shared Sub Main()

Dim s As String = "alTESTskdjfTESTTESTklaTESTsdf"

Console.WriteLine(StrCount(s, "TEST"))

End Sub

Public Shared Function StrCount(ByVal text As String, ByVal find As
String) As Int32

Return Regex.Split(text, find).Length - 1

End Function

End Class
'**
 
J

Jason Barnett

Thanks

Zoury said:
Hi Jason ! :O)


You could use the Regex.Split() method and look at the Length of the
string array being returned..

'**
Imports System.Text.RegularExpressions

Public Class MyApplication

Public Shared Sub Main()

Dim s As String = "alTESTskdjfTESTTESTklaTESTsdf"

Console.WriteLine(StrCount(s, "TEST"))

End Sub

Public Shared Function StrCount(ByVal text As String, ByVal find As
String) As Int32

Return Regex.Split(text, find).Length - 1

End Function

End Class
'**
 

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

Top