Skip to content

InStrRev

Returns the position of an occurrence of one string within another, from the end of the string.

Syntax: InStrRev( stringcheck, stringmatch [ , start [ , compare ] ] )

stringcheck
required String expression being searched.
stringmatch
required String expression being searched for.
start
optional Numeric expression that sets the starting position for each search. If omitted, -1 is used, which means that the search begins at the last character position. If start contains Null, an error occurs.
compare
optional Numeric value indicating the kind of comparison to use when evaluating substrings. If omitted, a binary comparison is performed. See settings below.

The compare argument can have the following values:

ConstantValueDescription
vbUseCompareOption-1Performs a comparison by using the setting of the Option Compare statement.
vbBinaryCompare0Performs a binary comparison.
vbTextCompare1Performs a textual comparison.

Return values:

IfInStrRev returns
stringcheck is zero-length0
stringcheck is NullNull
stringmatch is zero-lengthstart
stringmatch is NullNull
stringmatch is not found0
stringmatch is found within stringcheckPosition at which match is found
start > Len(stringcheck)0

INFO

The syntax for the InStrRev function is not the same as the syntax for the InStr function --- note the swapped order of the search arguments.

InStrRev will not find an instance of stringmatch unless the position of the end character of stringmatch is less than or equal to start.

Example

This example uses InStrRev to find the last occurrence of a substring.

vb
Debug.Print InStrRev("a.b.c", ".")       ' 4  — last dot
Debug.Print InStrRev("a.b.c", ".", 3)    ' 2  — last dot at or before position 3
Debug.Print InStrRev("a.b.c", "x")       ' 0  — not found

See Also

twinBASIC and LOGO copyright of "WaynePhillipsEA" author