Determine the last occurance of a string within another

Posted in: General vb.NET
By dePoPo
Mar 16, 2009 - 11:02:26 AM

This function returns the last occurance of a string within another one, comparable to the InStr function but working from the end of the string.


    ' --- Find the last occurance of a string
    '     simulair to instring, but scanning from the end of the string

    Function InstrLast(ByVal s_string As String, ByVal s_search As String) As Integer


        If Len(s_string) <= Len(s_search) Then
            ' --- search key is bigger then search source ...
            Return -1
        End If
 
        For f = Strings.Len(s_string) - Len(s_search) To 1 Step -1
            If Strings.Mid(s_string, f, Len(s_search)) = s_search Then
                Return f
            End If
        Next
 
        Return -1
 
    End Function


Visitor Comments