$FIND; $FIND

Finds the substring by value and returns an integer specifying where it ends in the string.

The outline

$FIND(string,substring,position)
$F(string,substring,position)
Copy the code

parameter

  • String Indicates the string to search for. It can be a variable name, a value, a string literal, or any valid CacheObjectScript expression, and can be parsed as a string.
  • Substring Substring to search for. It can be a variable name, a value, a string literal, or any valid CacheObjectScript expression, and can be parsed as a string.
  • Position Optional – The position in the target string where the search begins. It has to be a positive integer.

describe

$FIND returns an integer that specifies the end position of the substring in the string. $FIND searches a string for substrings. $FIND is case sensitive. If a substring is found, $FIND returns the integer position of the first character after the substring. If no substring is found, $FIND returns the value 0.

Because $FIND returns the position of the character after the substring, it returns 2 when the substring is a single character that matches the first character of the string $FIND. $FIND returns 1 when the substring is an empty string (” “).

You can include a location option to specify where to start the search. If position is greater than the number of characters in the string, $FIND returns the value 0.

$FIND calculates characters, not bytes. Therefore, it can be used with strings containing 8 – or 16-bit (Unicode) characters.

The sample

For example, if variable var1 contains the string “ABCDEFG” and variable var2 contains the string “BCD”, the following $find returns a value of 5 indicating the position of the character (” E “) following the string var2:

DHC-APP>SET var1="ABCDEFG",var2="BCD"
 
DHC-APP>WRITE $FIND(var1,var2)
5
Copy the code

The following example returns 4, the position of the character immediately to the right of the substring “FOR”.

DHC-APP>SET X="FOREST"
 
DHC-APP>WRITE $FIND(X,"FOR")
4
Copy the code

In the following example, $FIND searches for substrings that are not in the string, for null substrings, and for substrings that are the first character of the string. These examples return 0, 1, and 2, respectively:

DHC-APP>WRITE ! ,$FIND("aardvark"."z")
 
0DHC-APP>WRITE ! ,$FIND("aardvark"."")
 
1DHC-APP>WRITE ! ,$FIND("aardvark"."a")
 
2
Copy the code

The following example shows what happens when the string is an empty string:

DHC-APP>WRITE ! ,$FIND(""."z")
 
0DHC-APP>WRITE ! ,$FIND(""."")
 
1
Copy the code

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.

DHC-APP>SET X="EVERGREEN FOREST",Y="R"
 
DHC-APP>WRITE $FIND(X,Y,7)
14
Copy the code

In the following example, $FIND begins the search 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
Copy the code

The following example uses $FIND and $REVERSE to search from the end of a string. This example is the last example of finding 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
Copy the code

The following example indirectly returns 6 by name, the character position immediately to the right of the substring “THIS” :

DHC-APP>SET Y="x",x="""THIS IS A TEST"""
 
DHC-APP>WRITE $FIND(@Y."THIS")
6
Copy the code

Pay attention to


F I N D . FIND,
EXTRACT,
P I E C E . PIECE,
LIST

  • $FINDFinds the substring by value and returns the position.
  • $EXTRACTLocate the substring by position and return the substring value.
  • $PIECELocate a substring by a delimiter character or delimiter string and return a substring value.
  • $LISTOperates on specially encoded strings. It looks for substrings by substring counting and returns substring values.

The $FIND, $EXTRACT, $LENGTH, and $PIECE functions operate on standard strings. The various $LIST functions operate on encoded strings that are incompatible with standard strings. The only exceptions are the one – and two-parameter forms of $LIST, which take an encoded string as input but output individual element values as standard strings.

The agent of

$FIND could not identify the proxy pair. Proxy pairs are used to represent certain Chinese characters and support the Japanese JIS2004 standard. You can use the $WISWIDE function to determine if the string contains a proxy pair. The $WFIND function identifies and correctly parses the proxy pair. $FIND is the same as $WFIND. However, because $FIND is generally faster than $WFIND, $FIND is preferable for all cases where a proxy pair is unlikely to be encountered.