Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > String occurance


Reply
 
Thread Tools Display Modes
  #1  
Old 04-11-2002, 11:29 AM
d_hadzima
Guest
 
Posts: n/a
Default String occurance


Will someone please remind me of the fucntion that counts the number of occurance of a substring in a string.

Ex:

numberOfcommas = someFunction("there, are four, commas, in, this string", ",")

Thanks in advace
D
Reply With Quote
  #2  
Old 04-11-2002, 11:31 AM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

InStr() in a loop.
Reply With Quote
  #3  
Old 04-11-2002, 11:43 AM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default

Or use the InstrCount function from VBSpeed. Here.
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #4  
Old 04-11-2002, 11:44 AM
klabranche klabranche is offline
Junior Contributor
 
Join Date: May 2000
Location: Arizona
Posts: 288
Default

instead of a loop using InStr()

you could use the split function and return the UBOUND of the resulting array for the # of occurances....


Dim s As String
Dim results() As String

s = "There, are, three, comma's in this string"


results = Split(s, ",")

MsgBox (UBound(results))
Reply With Quote
  #5  
Old 04-11-2002, 11:45 AM
Jared Jared is offline
Centurion

Retired Moderator
* Guru *
 
Join Date: Oct 2000
Location: Ohio
Posts: 176
Default

I don't recall a VB function to count sub strings, but you can do this...


Dim strString As String
Dim aTemp() As String
Dim lCount As Long

strString = "there, are four, commas, in, this string"
aTemp = Split(strString, ",")
lCount = UBound(aTemp)

Msgbox "There are " & CStr(lCount) & " commas in that string."
__________________
Jared
Reply With Quote
  #6  
Old 04-11-2002, 11:46 AM
Ales Zigon's Avatar
Ales Zigon Ales Zigon is offline
Dead dog's ghost

Forum Leader
* Expert *
 
Join Date: Feb 2001
Location: Celje, Slovenia, Europe
Posts: 2,601
Default

Or something like this:

Code:
Private Sub Command1_Click()
Dim sString$
sString = "there, are four, commas, in, this string"
MsgBox NumberOfOcc(sString, ",")
End Sub

Function NumberOfOcc(ByVal MyString As String, sDelimiter As String) As Integer
Dim aDel$()
aDel = Split(MyString, sDelimiter)
NumberOfOcc = UBound(aDel)
End Function
__________________
Yes, MSDN comes with VB! Yes, you must have at least 25 post to have an avatar! No, you cant write your OS in VB! and NO, YOU CAN NOT DECOMPILE IT!

I'm sure there are things that are more important than me - I just can't thing of any...
Reply With Quote
  #7  
Old 04-11-2002, 11:46 AM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default re: klabranche

Good idea. So long as he isn't doing a lot of these in a loop. Split() is real slow.
Reply With Quote
  #8  
Old 04-11-2002, 11:52 AM
klabranche klabranche is offline
Junior Contributor
 
Join Date: May 2000
Location: Arizona
Posts: 288
Default

reboot...

you are right (split is slow) but probably not much slower than if you had to do a lot of looping.... speed vs. readability
Reply With Quote
  #9  
Old 04-11-2002, 12:08 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

Don't forget all the wonderful functions in this thread, along with
their relative speeds.
http://www.xtremevbtalk.com/show...threadid=13640
__________________
Posting Guidelines
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->