About   -   Contact   -   Purchase   -   Search   -   What's New

Search for the Last Occurrence of a Pattern in a String
This function is used to interrogate a string and locate to last position of a character or set of characters. This has many applications including filepath handling and multiline textbox interrogation. The function is passed the string to be interrogated and the search character(s). If the search characters do not occur in the string, then a value of zero is returned. The function code is as follows:
Function LastInStr(strText As String, strPattern _
  As String) As Integer

'<DESC>		Detects last occurrence of a string or
'		string pattern in a string</DESC>
'<RETURN>	Position of pattern in string
'<ARGS>		String to be interrogated
'		String pattern to search for</ARGS>
'<USAGE>	intStringPosition = LastInStr("c:\winnt\ _
'		system32\regsvr332.exe","/")</USAGE>

Dim intPos1 As Integer
Dim intPos2 As Integer

  intPos2 = 0
    intPos1 = intPos2
    intPos2 = InStr(intPos1 + 1, strText, strPattern)
  Loop While intPos2 > 0
  LastInStr = intPos1

End Function

Top of Page

Legal Notice

Ken Howe 2011