Find string's position
FIND returns the position at which the second input string first occurs in the first input string. The result is written into the output variable. If the second input string does not occur in the first input string, the value ZERO is returned.
Input
Input string
Case-sensitive string that is searched for in the input string
Output
if value > 0: position at which the string searched for is found, whereby 1 refers to the first character
if value = 0: search string not found
If this instruction is used with UTF-8 strings, please refer to the notes concerning UTF-8 strings under the data type STRING.
If the strings are longer than the length defined for the input variables (IN1 and IN2) in the declaration field “Type”, an error occurs (see sys_bIsCarry for error handling).
if a string applied at the input or output is an invalid string
if the input strings are longer than the length defined for the input variables in the field “Type”
if a string applied at the input or output is an invalid string
if the input strings are longer than the length defined for the input variables in the field “Type”
All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.
VAR
sInputString: STRING[5]:='ABCBC';
(*sample string*)
sSearchString: STRING[2]:='BC';
(*searched string*)
iFoundPos: INT:=0;
(*1st position found*)
END_VAR
In this example the input variables (sInputString and sSearchString) have been declared. Instead, you may enter the strings ('ABCBC.' and 'BC') directly into the function. The strings have to be put in inverted commas, both in the POU header and in the function.
sSearchString ('BC’) is searched in sInputString ('ABCBC’). The position of the first occurrence (2) is written into iFoundPos.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 4 ;
NETWORK_BODY
B(B_VARIN,,sInputString,18,1,20,3,);
B(B_VARIN,,sSearchString,18,2,20,4,);
B(B_VAROUT,,iFoundPos,25,1,27,3,);
B(B_F,FIND!,,20,0,25,4,,?DIN1?DIN2?C);
L(1,0,1,4);
END_NETWORK_BODY
END_NET_WORK
END_BODY
iFoundPos := FIND(sInputString, sSearchString);
LD |
sInputString |
FIND |
sSearchString |
ST |
iFoundPos |