473,787 Members | 2,928 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 #1
11 23274
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.Str ingBuilder(10 * 1024 * 1024)

For i As Integer = 0 To 1024 * 1024
sb.Append("0123 456789")
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.Reg ularExpression. 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.publi c.dotnet.langua ges.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.n l...
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

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

Similar topics

13
8760
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
13643
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
7738
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
3271
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
11979
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
9497
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
10363
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
10169
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...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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
4067
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
3
2894
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.