473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Is there a string function for counting occurances of substrings?

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.
Nov 21 '05 #1
2 5467
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
'**
--
Best Regards
Yanick
Nov 21 '05 #2
Thanks

"Zoury" <yanick_lefebvre at hotmail dot com> wrote in message
news:e8****************@TK2MSFTNGP09.phx.gbl...
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
'**
--
Best Regards
Yanick

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Xah Lee | last post by:
if i have mytext.replace(a,b) how to find out many many occurances has been replaced? Xah xah@xahlee.org ∑ http://xahlee.org/
6
by: jabailo | last post by:
Which would be faster for counting lines in a StreamReader: (a) iterate through a file using .ReadLine() and adding to a counter, i++ (b) doing a .ReadToEnd() and then using an IndexOf() method...
10
by: CodeRazor | last post by:
Is there a string method that allows gives you the number of times a substring appears in your string. Looping through my string performing IndexOf("substring",startPos), seems like overkill. ...
1
by: Chris Wertman | last post by:
Im a little stuck on an efficent way to do this. Say I have an array of strings , or a simple list whatever. Like this Del Rey Dell Ray DelRey Del Rey Del Rey Del Ray
8
by: girish | last post by:
Hi, I want to generate all non-empty substrings of a string of length >=2. Also, each substring is to be paired with 'string - substring' part and vice versa. Thus, gives me , , , , , ] etc....
13
by: Jonathan Wood | last post by:
According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear to...
8
by: rushik | last post by:
Hello, I am writing one application in which I am getting data as a string " 10 | 20 | 30 | 40 | 50" now my aim is to parse those string, split it by pipe '|' and get integer outputs in some...
4
by: sandvet03 | last post by:
I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to...
3
by: gh | last post by:
I have a string that is delimited with commas. Is there a function in C# that will break apart the string at each comma, so I can copy the value to a var? This is a .net v1.1.4 app. TIA
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.