Caché Basic Reference
InStrRev
[Home] [Back] [Next]
InterSystems: The power behind what matters   
Class Reference   
Search:    

Returns the position of the first occurrence of one string within another, searching from the end of string.
Synopsis
InStrRev(string1,string2[,start[,compare]])
Arguments
string1 String expression being searched.
string2 String expression being searched for.
start Optional —An integer that sets the starting position for the reverse direction search. The start position is counted from left to right (counting from 1); the search is done from right to left. Thus you should specify a positive integer specifying a start position to the right of the expected location of string2. The start position can be the actual position of string2. To search the entire string, from right to left, specify -1. If start is omitted, -1 is the default.
compare Optional — Numeric value indicating the kind of comparison to use when evaluating substrings. If omitted, a binary comparison is performed. See Description section for values.
Note:
The order of the arguments in the syntax for the InStrRev function is not the same as the InStr function syntax.
Description
The InStrRev function searches a string from right-to-left, and returns the location of the first occurrence of string2 encountered. The location returned is a positive integer counting from left-to-right, with the first (leftmost) character of the string being location 1. The location returned specifies the beginning (leftmost) character of string2.
If the start value is equal to or greater than the location of the last (rightmost) character of string2, InStrRev finds the first instance of string2, searching backwards from that point, and returns the location of the first (leftmost) character of string2. If the start value is less than the location of the last (rightmost) character of string2, InStrRev returns 0. If the start value is -1 or omitted, InStrRev searches the entire string and returns the first string2 location found, searching backwards from the end of the string. If the start value is greater than the string1 length, InStrRev returns 0. If the start value is a negative number other than -1, InStrRev returns 0.
The compare argument can have the following values:
Constant Value Description
vbBinaryCompare 0 Perform a binary comparison.
vbTextCompare 1 Perform a textual comparison.
The InStrRev function returns the following values:
If InStr Returns
string1 is zero-length 0
string2 is zero-length start
string2 is not found 0
string2 is found within string1 Position at which match is found
start > Len(string2) 0
Examples
The following examples use InStrRev to search a string:
Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP"            ' String to search in.
SearchChar = "P"                        ' Search for "P".

' A binary comparison starting at position 10. Returns 9.
Println InstrRev(SearchString, SearchChar, 10, 0)   

' A textual comparison starting at the last position. Returns 12.
Println InstrRev(SearchString, SearchChar, -1, 1)

' Comparison is binary by default (last argument is omitted). Returns 0.
Println InstrRev(SearchString, SearchChar, 8)
 
The following example shows the use of the start argument.
Println "-1: ",InStrRev("abcdefg","bc",-1)  ' Returns 2
Println "0: ",InStrRev("abcdefg","bc",0)    ' Returns 0
Println "1: ",InStrRev("abcdefg","bc",1)    ' Returns 0
Println "2: ",InStrRev("abcdefg","bc",2)    ' Returns 0
Println "3: ",InStrRev("abcdefg","bc",3)    ' Returns 2
Println "6: ",InStrRev("abcdefg","bc",6)    ' Returns 2
Println "7: ",InStrRev("abcdefg","bc",7)    ' Returns 2
Println "8: ",InStrRev("abcdefg","bc",8)    ' Returns 0
 
See Also