Article Directory
-
Chapter 16 Caché Function Collection $FIND Function - Outline
- description
- Example
- note
Chapter 16 Caché Function Collection $FIND Function
Find the substring by value, and return an integer, specifying its end position in the string.
Outline
$FIND(string,substring,position)
$F(string,substring,position)
parameter
- string The target string to be searched. It can be a variable name, numeric value, string literal or any valid CachéObjectScript expression, which can be parsed as a string.
- substring The substring to search for. It can be a variable name, numeric value, string literal or any valid CachéObjectScript expression, which can be parsed as a string.
- position Optional-the position in the target string to start the search. It must be a positive integer.
description
$FIND
Returns an integer that specifies the end position of the substring in the string. $FIND
Search for a substring in the string. $FIND
Case sensitive. If a substring is found, $FIND
the integer position of the first character after the substring is returned. If the substring is not found, the $FIND
value 0 is returned.
Because $FIND
the position of the character after the substring is returned, when the substring is $FIND
a single character that matches the first character of the string, 2 is returned. When the substring is an empty string ( “”
), $FIND
1 is returned.
You can include a location option to specify the starting location of the search. If position is greater than the number of characters in the string, the $FIND
value 0 is returned.
$FIND
Calculate characters, not bytes. Therefore, it can be used with strings containing 8-bit or 16-bit (Unicode) characters.
Example
For example, if the variable var1
contains the string " ABCDEFG
" and the variable var2
contains the string " BCD
", the following $find
return value 5 indicates the position var2
of the character (" E
") after the string :
DHC-APP>SET var1="ABCDEFG",var2="BCD"
DHC-APP>WRITE $FIND(var1,var2)
5
The following example returns 4, that is, the position of the character is immediately to FOR
the right of the substring " ".
DHC-APP>SET X="FOREST"
DHC-APP>WRITE $FIND(X,"FOR")
4
In the following example, $FIND
the substring that is not in the string, the null substring, and the substring that is the first character of the string will be searched. These examples return 0, 1, and 2:
DHC-APP>WRITE !,$FIND("aardvark","z")
0
DHC-APP>WRITE !,$FIND("aardvark","")
1
DHC-APP>WRITE !,$FIND("aardvark","a")
2
The following example shows what happens when the string is an empty string:
DHC-APP>WRITE !,$FIND("","z")
0
DHC-APP>WRITE !,$FIND("","")
1
The following example returns 14, that is, the position of the character is immediately X
to R
the right of the first occurrence of " " after the seventh character in the middle .
DHC-APP>SET X="EVERGREEN FOREST",Y="R"
DHC-APP>WRITE $FIND(X,Y,7)
14
In the example below, $FIND
the search starts after the last character in the string. It returns zero (0):
DHC-APP>SET X="EVERGREEN FOREST",Y="R"
DHC-APP>WRITE $FIND(X,Y,20)
0
The following example uses $FIND
and $REVERSE
performs a search operation from the end of the string. This example finds the last example of a string in a line of text. It returns the position of the string as 33:
DHC-APP>SET line="THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG."
DHC-APP>SET position=$LENGTH(line)+2-$FIND($REVERSE(line),$REVERSE("THE"))
DHC-APP>WRITE "The last THE in the line begins at ",position
The last THE in the line begins at 33
The following example uses the name to indirectly return 6, which is THIS
the character position immediately to the right of the substring " ":
DHC-APP>SET Y="x",x="""THIS IS A TEST"""
DHC-APP>WRITE $FIND(@Y,"THIS")
6
note
$FIND, $EXTRACT, $PIECE, $LIST
$FIND
Find the substring by value and return the position.$EXTRACT
Position the substring by position and return the substring value.$PIECE
Locate the substring by the separator character or separator string, and return the substring value.$LIST
Operate on specially encoded strings. It finds the substring by counting the substring and returns the value of the substring.
$FIND
, $EXTRACT
, $LENGTH
And $PIECE
functions that operate on the standard string. Various $LIST
functions operate on encoded strings that are incompatible with standard strings. The only exception is $LIST
the one-parameter and two-parameter forms, which take an encoded string as input, but output a single element value as a standard string.
Surrogate pair
$FIND
The surrogate pair cannot be recognized. The surrogate pair is used to represent certain Chinese characters and supports the Japanese JIS2004 standard. You can use $WISWIDE
functions to determine whether the string contains surrogate pairs. $WFIND
The function can identify and correctly parse the surrogate pair. $FIND
And the $WFIND
same. However, since it $FIND
is usually $WFIND
faster, it $FIND
is preferable for all situations where it is unlikely to encounter a surrogate pair .