Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRecursively find the last occurrance of a substring without using InStrRev
KeywordsInStrRev, last occurrance
CategoriesStrings
 
Thanks to Jean Loiselle.

InStrRev does this in VB6 but in previous versions of VB you needed to use your own function similar to this one. (Yes it is a bit odd that this example is saved in VB6 format but is only needed in versions before VB6. Copy and paste the function's code if you use an earlier version. Use InStrRev in VB6.)

This function searches the string for the substring. If it finds it, the function calls itself to search the rest of the string for another occurrance. It continues until it cannot find the substring any more.

 
Public Function FndLast(t1 As String, t2 As String, _
    Optional ft As Integer = 0) As Integer
Dim cl As Integer

    If ft = 1 Then cl = Len(t2) - 1
    If InStr(t1, t2) = 0 Then
        Exit Function
    Else
        cl = cl + InStr(t1, t2) + FndLast(Mid$(t1, _
            InStr(t1, t2) + Len(t2)), t2, 1)
    End If
    FndLast = cl
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated