473,387 Members | 1,516 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,387 software developers and data experts.

String instance count

Jon
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
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 #1
11 23225
Cor
Hi Jon,
I am too always looking for that, but when I did look at your code I thought
this stupid
\\\\\\\
Dim a As String = "abaaa abbba babaabaabaab"
Dim b As String() = Split(a, "ab")
MessageBox.Show((b.Length - 1).ToString)
///////
When you don't get a better answer you can try it, doing some little tests
it did work.
It is stupid of course.
Cor
Nov 20 '05 #2
"Cor" <no*@non.com> scripsit:
I am too always looking for that, but when I did look at your code I thought
this stupid
\\\\\\\
Dim a As String = "abaaa abbba babaabaabaab"
Dim b As String() = Split(a, "ab")
MessageBox.Show((b.Length - 1).ToString)
///////
When you don't get a better answer you can try it, doing some little tests
it did work.
It is stupid of course.


Yes, it works. But I think it's not the best solution if you split a 10
MB string.

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
Cor
Herfried,
The other samples does as far as I can see a loop from 1 increment per
character each time.
I am not so sure if that is faster,
But ok lets make a deal, do you type a 10Mb string, than will I make the
testprogram.
Cor
Nov 20 '05 #4
Cor,
When I need a 10Mb string, I normally use a loop with a StringBuilder. As I
suspect posting a 10M string to the newsgroup might be blocked ;-)

Something like:

Dim sb As New System.Text.StringBuilder(10 * 1024 * 1024)

For i As Integer = 0 To 1024 * 1024
sb.Append("0123456789")
Next

Dim s As String = sb.ToString()

Sometimes I will use a System.Random object to indirectly create random text
to append instead of the fixed values.

A week or two ago the topic of counting occurrences of delimiters in strings
came up in the C# newsgroup, someone suggested using a
System.Text.RegularExpression.RegEx object. One of the individuals did some
testing, the RegEx method was about 100 times slower than the for loop. They
did not try the Split method. This was based on occurrences of individual
characters, not occurrences of strings. See "string manipulation" from 25
Sept 2003 in the microsoft.public.dotnet.languages.csharp newsgroup if you
are interested.

Out of curiosity it would be interesting to see what the difference between
splitting, counting & reg expressions performance wise... On both VB.NET
2002 & VB.NET 2003...

Hope this helps
Jay

"Cor" <no*@non.com> wrote in message
news:3f***********************@reader22.wxs.nl...
Herfried,
The other samples does as far as I can see a loop from 1 increment per
character each time.
I am not so sure if that is faster,
But ok lets make a deal, do you type a 10Mb string, than will I make the
testprogram.
Cor

Nov 20 '05 #5
"Cor" <no*@non.com> scripsit:
The other samples does as far as I can see a loop from 1 increment per
character each time.
I am not so sure if that is faster,
But ok lets make a deal, do you type a 10Mb string, than will I make the
testprogram.


Splitting a 10 MB sting will increase memory usage for the application
to 20 MB. That's what I wanted to say.

:-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
Cor
Hi Jay,
Me too,
I shall try to make a test and give the results here.
But I don't have vb2002 and I am always to lazy to make a regex.
But I have a link, someone did send for a regex maker, maybe I can use that.
Otherwise it is only the two methods, (maybe three so that I can use
Microsoft Visual basic too)
I thought that Jeremy Cowles said in the chat that they where so terrible
slow.

I make that string myself of course, but look at the post I send to
Herfried.

Normaly you see the results tomorrow.
Cor
Nov 20 '05 #7
Cor
Hi Herfried,
I forgot to tell, when you are ready with typing that teststring from 10Mb.
please don't zip it in the post, just paste it in.
With those problems from the newserver from Microsoft, I think a zip file
never arives.
Cor
Nov 20 '05 #8
"Cor" <no*@non.com> scripsit:
I forgot to tell, when you are ready with typing that teststring from 10Mb.
please don't zip it in the post, just paste it in.
With those problems from the newserver from Microsoft, I think a zip file
never arives.


I think there will be a size limitation...

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #9
Cor
Herfried,
I have seen you are very quick in typing. That will give no size problem.
Cor
Nov 20 '05 #10
"Jon" <ru******@msn.com> wrote in
news:vn************@corp.supernews.com:
I want to count the number of instances of a certain string(delimiter)


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

MsgBox("Number of matches = " & CStr(rx.Matches(sSource).Count))
'////
Nov 20 '05 #11
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
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...
4
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:...
4
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...
4
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
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
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...
5
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...
8
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...
3
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.