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
Printable View
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
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
Or use InStr
You would call the function: GetOccurrence("123456,67,78,75,86,99", ",", 0)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
Cheers,
P.
Or, for VB6 (and higher?) a one-liner!
Usage:Code:Private Function GetOccurances(strSearchMe As String, strToFind As String)
GetOccurances = UBound(Split(strSearchMe, strToFind, , vbTextCompare))
End Function
Enjoy!Code:Count= GetOccurances("123456,67,78,75,86,99", ",")
By the way... Did you notice the function doesn't even use a variabele? ;)
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?
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?