-
Actually, I just tested it on a 3 million character string and it is a lot slower than InStr.
But if I want to find the 5000th occurrence of "X", how can I make it faster?
Latest version:
Code:
Public Function InStr2(ByVal sString As String, ByVal sSearch As String, Optional ByVal lStart As Long = 1, Optional ByVal lOccurrence As Long = 1, Optional ByVal bMatchCase As Boolean = False) As Long
'If sSearch is negative, the search will occur from right to left
'and will return the position from the right that the search string starts
Dim lPos As Long
Dim lCntr As Long
Dim lLength As Long
Dim lSearchLen As Long
Dim sTemp As String
Dim lNum As Long
Dim bReverse As Boolean
lLength = Len(sString)
lSearchLen = Len(sSearch)
bReverse = lStart < 0
lStart = Abs(lStart)
If (lLength = 0) Or (lSearchLen = 0) Or (lSearchLen > lLength) Then
InStr2 = 0
Exit Function
End If
If Not bMatchCase Then
sString = UCase$(sString)
sSearch = UCase$(sSearch)
End If
If bReverse Then
For lCntr = lLength To 1 Step -1
sTemp = Mid$(sString, lCntr, lSearchLen)
If sTemp = sSearch Then
lPos = lLength - (lCntr - 1)
lNum = lNum + 1
If lNum = lOccurrence Then Exit For
End If
Next lCntr
Else
For lCntr = 1 To lLength
sTemp = Mid$(sString, lCntr, lSearchLen)
If sTemp = sSearch Then
lPos = lCntr
lNum = lNum + 1
If lNum = lOccurrence Then Exit For
End If
Next lCntr
End If
If lNum = lOccurrence Then
InStr2 = lPos
Else
InStr2 = 0
End If
End Function
-
Happy Birthday Skitchen8.
Okay, let's just agree that it's not nice to slag off other peoples answers and I'll be happy.:D
I don't have VB on this machine, but this should give what you want for the forward search. Use InstrRev for the backwards search.
The If.. Then statement in the middle isn't really needed as the loop will end eventually, but it might provide a speed increase if you are unsure if there are that many occurences of the search string.
Code:
lPos=0
For lCntr = 1 To lOccurence
lPos = instr(lPos+1,sString, sSearch)
If lPos=0 then
Exit For
End If
Next lCntr
I hope this helps,
SD
-
So simple, yet so effective.
When did InStrRev pop up? I can't believe I have never even heard of it.
Thanks, and I'll try to play nice from now on.
-
Cool. Peace man.
InstrRev came in with VB 6.0 and all the other split functions and the like.
Have fun :D
SD
-
Well...
Stevess you said "I dont know what this code does" right..
well, look at the comments that the person who wrote the code before had typed
"This function accepts as inputs the number of input symbols, an
array where the input symbols are stored and an output array
where the resulting output symbols are stored."
does that answer your question? :p
-
Actually, that has very little to do with what it does. Honestly, if I tried, even a little, to figure it out I could probably grasp it. My whole point was that it pretty much worked as supllied so I never took the time. It was just a small piece of a large project and I knew that I would probably never use it again.
If someone had just given me the algorithms and said "This is how it works on paper, translate it into code.", I would have been forced to learn exactly what it was doing.
There is a lot more to generating these bar codes, the rest of it I pretty coded myself using a Guide supplied by Australia Post.