Finds a substring by value and returns an integer specifying its end position in the string.
||The target string that is to be searched. It can be a variable name, a numeric value, a string literal, or any valid ObjectScript expression that resolves to a string.
||The substring that is to be searched for. It can be a variable name, a numeric value, a string literal, or any valid ObjectScript expression that resolves to a string.
||Optional A position within the target string at which to start the search. It must be a positive integer.
You can include the position
option to specify a starting position for the search. If position
is greater than the number of characters in string
returns a value of 0.
counts characters, not bytes. Therefore, it can be used with strings containing 8-bit or 16-bit (Unicode) characters. For further details on InterSystems IRIS Unicode support, refer to Unicode
in Using ObjectScript
For example, if variable var1
contains the string ABCDEFG and variable var2
contains the string BCD, the following $FIND
returns the value 5, indicating the position of the character (E) that follows the var2
The following example returns 4, the position of the character immediately to the right of the substring FOR.
In the following examples, $FIND
searches for a substring that is not in string
, for a null substring, and for a substring that is the first character of string
. The examples return 0, 1, and 2, respectively:
WRITE !,$FIND("aardvark","z") ; returns 0
WRITE !,$FIND("aardvark","") ; returns 1
WRITE !,$FIND("aardvark","a") ; returns 2
The following examples show what happens when string
is a null string:
WRITE !,$FIND("","z") ; returns 0
WRITE !,$FIND("","") ; returns 1
The following example returns 14, the position of the character immediately to the right of the first occurrence of R after the seventh character in X
SET X="EVERGREEN FOREST",Y="R"
In the following example, $FIND
begins its search after the last character in string. It returns zero (0):
SET X="EVERGREEN FOREST",Y="R"
The following example uses $FIND
to perform a search operation from the end of the string. This example locates the last example of a string within a line of text. It returns the position of that string as 33:
SET line="THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG."
WRITE "The last THE in the line begins at ",position
The following example uses name indirection to return 6, the position of the character immediately to the right of the substring THIS:
SET Y="x",x="""THIS IS A TEST"""
$FIND, $EXTRACT, $PIECE, and $LIST
locates a substring by value and returns a position.
locates a substring by position and returns the substring value.
locates a substring by a delimiter character or delimiter string, and returns the substring value.
operates on specially encoded strings. It locates a substring by substring count and returns the substring value.
, and $PIECE
functions operate on standard character strings. The various $LIST
functions operate on encoded character strings, which are incompatible with standard character strings. The sole exception is the one-argument and two-argument forms of $LIST
, which take an encoded character string as input, but output a single element value as a standard character string.
does not recognize surrogate pairs. Surrogate pairs are used to represent some Chinese characters and to support the Japanese JIS2004 standard. You can use the $WISWIDE
function to determine if a string contains a surrogate pair. The $WFIND
function recognizes and correctly parses surrogate pairs. $FIND
are otherwise identical. However, because $FIND
is generally faster than $WFIND
is preferable for all cases where a surrogate pair is not likely to be encountered.