473,799 Members | 2,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String instance count

Jon
I want to count the number of instances of a certain string(delimite r) in
another string. I didn't see a function to do this in the framework (if
there is, please point me to it). If not, could someone let me know if the
method I've used below is efficient or if there is a better way to do it, as
these will be rather large strings I'm searching in. Thanks

Public Shared Function CountDelimiter( ByVal strInput As String, ByVal
strDelimiter As String) As Int32
Dim iStart As Int32, iCount As Int32, iResult As Int32

'Set our vars to base values
iStart = 1
iCount = 0

Do
'iResult becomes the position where delimiter is found. If 0, not
found.
iResult = InStr(iStart, strInput, strDelimiter)
If iResult = 0 Then Exit Do
'Increment our count var for each time it is found
iCount += 1
'Increment our next start position to be the next char after the
currently found position
iStart = iResult + 1
Loop

Return iCount
End Function

--
--------
Jon Rosenberg
Nov 20 '05
11 23276
"Jon" <ru******@msn.c om> wrote in
news:vn******** ****@corp.super news.com:
I want to count the number of instances of a certain string(delimite r)


One method would be to use a regular expression and then check the
Matches.Count property:
'\\\\
Dim sSource As String = "abbabaabbababa babbababbabbaab ababbbabbababab babab"
Dim rx As New System.Text.Reg ularExpressions .Regex("a")

MsgBox("Number of matches = " & CStr(rx.Matches (sSource).Count ))
'////
Nov 20 '05 #11
therealpmuk
1 New Member
I also have an interest in this, needing to check for (and count) the number of occurrences of various errors in an error.log file.

The files can get quite large, but I have achieved the following results with a filesize of just over 11 Meg:-


Count 79608

TimeBySplit 22187500 Long
TimeByLoop 28750000 Long
TimeByRegEx 1093750 Long


Count 113

TimeBySplit 2031250 Long
TimeByLoop 28125000 Long
TimeByRegEx 156250 Long

Timing done using System.DateTime .Now.Ticks.

As you can see, the Regular Expression method consistently yields the fastest results.
Jul 27 '06 #12

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

Similar topics

13
8762
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two "i" and two "s". The function should also handle if String A and B have huge values, like two big dictionary. What's the best approach to achieve this with the best performance? what's the Big O then? I am thinking to put A and B into two...
4
9312
by: francescomoi | last post by:
Hi. I'm trying to remove some characters within a string and substitute others. For instance, I want to convert: John's new house, great ---> Johns-new-house-great I tried with: ----------------//------------------ int main() {
4
13644
by: Jason Gleason | last post by:
What's the most efficient way to get the number of occurences of a certain string in another string..for instance i'm using the following code right now... private int CharacterCounter(String text,String Character) { int count = 0;
4
55416
by: thomaz | last post by:
Hi.... There is a string method to count the total number of a specified character in a string. EX: count the total of (*) in a string *** Test *** Thanks....
6
2822
by: Chris Simmons | last post by:
I know that a String is immutable, but I don't understand why this piece of code fails in nUnit: // BEGIN CODE using System; class Test { public static void Main( String args )
10
2861
by: Jon | last post by:
I want to count the number of instances of a certain string(delimiter) in another string. I didn't see a function to do this in the framework (if there is, please point me to it). If not, could someone let me know if the method I've used below is efficient or if there is a better way to do it, as these will be rather large strings I'm searching in. Thanks Public Shared Function CountDelimiter(ByVal strInput As String, ByVal...
5
7741
by: jan axelson | last post by:
My application is using RegisterDeviceNotification() to detect attachment and removal of a USB HID-class device. The form is receiving WM_DEVICECHANGE messages with wParam set to DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE. I want to identify the device that has arrived or been removed by examining the dbcc_name member of the DEV_BROADCAST_DEVICEINTERFACE structure.
8
3273
by: Oenone | last post by:
Is it possible to create an object which can have methods and properties, but which can also be treated as a string? I'm trying to create a wrapper around the IIS Request.Form object which behaves the same as the classic ASP object behaved. This will allow me to quickly get a large amount of code up and running (I can then tweak it to not require this wrapper at a more leisurely pace). When queried directly, this object returns a...
3
26228
by: Kuups | last post by:
Hi! I have a question regarding the count if character within a string like for example I have a string of e.g. 123#123# I would like to determine what is the code? of getting the # sign
9
11981
by: senfo | last post by:
I realize it's Friday and I'm probably already on vacation for the remainder of the day; but, I have a really, really stupid question. Is there a bug in the .NET 2.0 Framework in regards to the LastIndexOf() method or am I just not understanding something that should be obvious? string myString = "This is a test"; int index = myString.LastIndexOf(' ', 0, 6); The second line throws an ArgumentOutOfRangeException exception, which says,...
0
9687
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
9543
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
10488
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
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10029
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...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
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...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
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

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.