|
-
Nov 21st, 2000, 06:54 AM
#1
hi !
is there any function which returns the no of ocurances of a sting in another.
eg, 123456,67,78,75,86,99
i want to find how many commas are there in this string
thanx
-
Nov 21st, 2000, 07:08 AM
#2
_______
<?>
Code:
Public Function GetCount(Character As String, FindMe As String)
Dim myLen As Integer
Dim iCount As Integer
Dim FoundMe As Integer
myLen = Len(Character)
For iCount = 1 To myLen
If Left(Character, 1) = FindMe Then
FoundMe = FoundMe + 1
End If
Character = Right(Character, (myLen - iCount))
Next
GetCount = FoundMe
End Function
Private Sub Command1_Click()
Dim myString As String
myString = "123456,67,78,75,86,99"
MsgBox GetCount(myString, ",")
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Nov 21st, 2000, 08:24 AM
#3
Fanatic Member
Or use InStr
Code:
Function GetOccurrence(strToSearch As String, strToFind As String, iFound As Integer) As Integer
Dim iPos as Integer, strName as String
Do While InStr(iPos + 1, strToSearch, strToFind) <> 0
iFound = iFound + 1
iPos = InStr(iPos + 1, strToSearch, strToFind)
Loop
Return iFound
End Function
You would call the function: GetOccurrence("123456,67,78,75,86,99", ",", 0)
Cheers,
P.
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 21st, 2000, 11:21 AM
#4
Or, for VB6 (and higher?) a one-liner!
Code:
Private Function GetOccurances(strSearchMe As String, strToFind As String)
GetOccurances = UBound(Split(strSearchMe, strToFind, , vbTextCompare))
End Function
Usage:
Code:
Count= GetOccurances("123456,67,78,75,86,99", ",")
Enjoy!
-
Nov 21st, 2000, 11:26 AM
#5
By the way... Did you notice the function doesn't even use a variabele? 
-
Nov 21st, 2000, 11:32 AM
#6
Fanatic Member
You're right - its an encapsulation of InStr - far too easy to use and no variables - pah! Must be for wimps!
Good spot.
P.
P.S. Can you see my Access 97 roots showing?
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 21st, 2000, 11:40 AM
#7
Fanatic Member
You're right - its an encapsulation of InStr - far too easy to use and no variables - pah! Must be for wimps!
Good spot.
P.
P.S. Can you see my Access 97 roots showing?
Not nearly so tired now...
Haven't been around much so be gentle...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|