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
 
 
 
 
 
TitleWrite an InStrRev function for VB 5
DescriptionThis example shows how to write an InStrRev function for VB 5. In VB 6, use the built in InStrRev function.
KeywordsInStrRev, VB5, Visual Basic 5
CategoriesStrings
 
Thanks to Christopher McGrath.

Thanks also to Roger Gilchrist for noticing that my original example called VB 6's InStrRev function instead of vb5InstrRev (my fault, not Christopher's). It was a pretty silly example written that way!

The vb5InstrRev function starts at the end of the string and works its way backwards to see if it can find the target string.

 
Public Function vb5InstrRev(ByVal StringCheck As String, _
    StringMatch As String, Optional Start As Long = -1, _
    Optional Compare As VbCompareMethod = vbBinaryCompare) _
    As Long
Dim lStartPoint As Long
Dim lEndPoint As Long
Dim lSearchLength As Long
Dim lCtr As Long
Dim sWkg As String

    vb5InstrRev = 0

    If Len(StringMatch) > Len(StringCheck) Then Exit _
        Function
    If Start < -1 Or Start = 0 Or Start > Len(StringCheck) _
        Then Exit Function

    lSearchLength = Len(StringMatch)
    lStartPoint = IIf(Start = -1, Len(StringCheck), Start)
    lEndPoint = 1

    For lCtr = lStartPoint To lEndPoint Step -1
        sWkg = Mid(StringCheck, lCtr, lSearchLength)
        If StrComp(sWkg, StringMatch, Compare) = 0 Then
            vb5InstrRev = lCtr
            Exit Function
        End If
    Next lCtr
End Function
 
See also:

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated