Chapter 16 Caché Function Collection $FIND Function

Article Directory

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

$FINDReturns an integer that specifies the end position of the substring in the string. $FINDSearch for a substring in the string. $FINDCase sensitive. If a substring is found, $FINDthe integer position of the first character after the substring is returned. If the substring is not found, the $FINDvalue 0 is returned.

Because $FINDthe position of the character after the substring is returned, when the substring is $FINDa single character that matches the first character of the string, 2 is returned. When the substring is an empty string ( “”), $FIND1 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 $FINDvalue 0 is returned.

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

Example

For example, if the variable var1contains the string " ABCDEFG" and the variable var2contains the string " BCD", the following $findreturn value 5 indicates the position var2of 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 FORthe right of the substring " ".

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

In the following example, $FINDthe 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 Xto Rthe 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, $FINDthe 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 $FINDand $REVERSEperforms 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 THISthe 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

  • $FINDFind the substring by value and return the position.
  • $EXTRACTPosition the substring by position and return the substring value.
  • $PIECELocate the substring by the separator character or separator string, and return the substring value.
  • $LISTOperate on specially encoded strings. It finds the substring by counting the substring and returns the value of the substring.

$FIND, $EXTRACT, $LENGTHAnd $PIECEfunctions that operate on the standard string. Various $LISTfunctions operate on encoded strings that are incompatible with standard strings. The only exception is $LISTthe 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

$FINDThe surrogate pair cannot be recognized. The surrogate pair is used to represent certain Chinese characters and supports the Japanese JIS2004 standard. You can use $WISWIDEfunctions to determine whether the string contains surrogate pairs. $WFINDThe function can identify and correctly parse the surrogate pair. $FINDAnd the $WFINDsame. However, since it $FINDis usually $WFINDfaster, it $FINDis preferable for all situations where it is unlikely to encounter a surrogate pair .

Guess you like

Origin blog.csdn.net/yaoxin521123/article/details/108428299