473,941 Members | 15,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5502
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.Reg ularExpressions

Public Class MyApplication

Public Shared Sub Main()

Dim s As String = "alTESTskdjfTES TTESTklaTESTsdf "

Console.WriteLi ne(StrCount(s, "TEST"))

End Sub

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

Return Regex.Split(tex t, find).Length - 1

End Function

End Class
'**
--
Best Regards
Yanick
Nov 21 '05 #2
Thanks

"Zoury" <yanick_lefebvr e at hotmail dot com> wrote in message
news:e8******** ********@TK2MSF TNGP09.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.Reg ularExpressions

Public Class MyApplication

Public Shared Sub Main()

Dim s As String = "alTESTskdjfTES TTESTklaTESTsdf "

Console.WriteLi ne(StrCount(s, "TEST"))

End Sub

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

Return Regex.Split(tex t, 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
6276
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
1719
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 to count the occurances of \r\n (c) doing a .ReadToEnd() and using a RegEx to count the number of occurances of \r\n
10
5423
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. thank you. CR
1
1309
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
5064
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. Similarly, 'abcd' should give me , , , , , ,, , , , , , ,] I've tried the following but i cant prevent duplicates and i'm missing
13
2662
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 modify s. s.Trim('\r'); While the follow code DOES modify s. s = s.Trim(\r');
8
1902
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 array or other data structure..... do anybody worked in the same functionality, then please let me know. Thanks,
4
2814
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 find a chosen substring lets say "cat" and then replace it with a difrent inputed substring say "dog". Tried to get as far as i could on my own but need some help, sugestions, snippets or guidence. # include<stdio.h> # include<math.h> #...
3
2131
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
10134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9860
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6080
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4487
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.