Find string's position
FIND_AFTER_POS returns the position at which the second input string IN2 occurs in the first input string IN1 beginning from the start position P. 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
Start position of the input string to be searched, where 1 is the first character of the string
Output
Position at which the string searched for is found
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 a string applied at the input or output is an invalid string
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
bStart: BOOL:=FALSE;
sInputString: STRING[32]:='A better life. A better world.';
sSearchString: STRING[32]:='A';
iStartPos: INT:=8;
iFoundPos: INT:=0;
END_VAR
In this example the input variables sSearchString and sInputString have been declared. Instead, you may enter the strings directly into the function. The strings have to be put in inverted commas, both in the POU header and in the function.
sSearchString is searched in sInputString beginning from start position 8. The position of the first occurrence after position 8 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 := 7 ;
NETWORK_BODY
B(B_F,E_FIND_AFTER_POS!,,19,1,28,7,,?DEN?DIN1?DIN2?DP?AENO?C);
B(B_VARIN,,sInputString,17,3,19,5,);
B(B_VARIN,,sSearchString,17,4,19,6,);
B(B_VARIN,,iStartPos,17,5,19,7,);
B(B_VAROUT,,iFoundPos,28,3,30,5,);
B(B_CONTACT,,bStart,13,2,15,4,);
L(1,3,13,3);
L(15,3,19,3);
L(1,0,1,7);
END_NETWORK_BODY
END_NET_WORK
END_BODY
IF bStart then
iFoundPos:= FIND_AFTER_POS(sInputString, sSearchString);
END_IF;