 |

04-11-2002, 11:29 AM
|
|
|
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
|
|

04-11-2002, 11:31 AM
|
 |
Keeper of foo
Retired Moderator * Guru *
|
|
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
|
|
|

04-11-2002, 11:43 AM
|
 |
ISearchGoogle
Retired Moderator * Expert *
|
|
Join Date: May 2001
Location: england
Posts: 6,321
|
|
Or use the InstrCount function from VBSpeed. Here.
|
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
|

04-11-2002, 11:44 AM
|
|
Junior Contributor
|
|
Join Date: May 2000
Location: Arizona
Posts: 288
|
|
|
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))
|
|

04-11-2002, 11:45 AM
|
|
Centurion
Retired Moderator * Guru *
|
|
Join Date: Oct 2000
Location: Ohio
Posts: 176
|
|
|
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
|

04-11-2002, 11:46 AM
|
 |
Dead dog's ghost
Forum Leader * Expert *
|
|
Join Date: Feb 2001
Location: Celje, Slovenia, Europe
Posts: 2,601
|
|
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...
|

04-11-2002, 11:46 AM
|
 |
Keeper of foo
Retired Moderator * Guru *
|
|
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
|
|
re: klabranche
|
Good idea. So long as he isn't doing a lot of these in a loop. Split() is real slow.
|
|

04-11-2002, 11:52 AM
|
|
Junior Contributor
|
|
Join Date: May 2000
Location: Arizona
Posts: 288
|
|
|
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
|
|

04-11-2002, 12:08 PM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|