%RVSTR

Search for the last occurrence of a substring within a string

WSupported on Windows
USupported on Unix
VSupported on OpenVMS
NSupported in Synergy .NET
position = %RVSTR(start, string, substring[, position2])

or

xcall RVSTR(start, string, substring, position2)

Return value

position

The position of the rightmost occurrence of substring within string. (i)

If any of the following conditions are true, the result is 0:

If substring is null but string is not, the result is 1. If position2 is specified, the result is also returned in that variable.

Arguments

start

The position in the specified string at which the search will end. (n)

string

The string in which to search for the substring. (a)

substring

The substring for which to search. (a)

position2

(optional) A variable in which to return the starting position of the last occurrence of substring. If RVSTR can’t find an exact match of the substring value, position2 is returned with a value of 0 to indicate that the search failed. (n)

Discussion

Searching from right to left, %RVSTR returns the position of the rightmost occurrence of a substring within a string.

%RVSTR searches for substring in string from position one (the last character of string) through start. The length of substring must be less than or equal to the length of string.

To search from left to right and find the first occurrence of the string, use %INSTR.

Examples

The example below checks whether a data record contains a specific string. The string may be in the record several times, but we’re looking for the most recent entry. The function returns zero if the string is not found or the record position if the string is found.

function chk_rec 
    a_data      ,a 
    a_string    ,a 
proc 
    freturn %rvstr(1, a_string, a_data) 
endfunction

As a second example, assume your data division looks like this:

record
    codes               ,a30,   "help.add.sub.mul.div"
    loc                 ,d3
    target              ,a3,    "sub"

The following subroutine returns a loc value of 18, because the last d appears at character position 18 in the string stored in the codes variable.

xcall rvstr(4, codes, "d", loc)