Copy characters from a middle position
MID copies L characters of the string IN starting at position P with 1 denoting the first character of the string. The result is written into the output variable.
Input
Input string
Number of input string's characters that are copied
Start position of the input string to be copied, where 1 is the first character of the string
Output
Copied string
If this instruction is used with UTF-8 strings, please refer to the notes concerning UTF-8 strings under the data type STRING.
The sum of start position and number of characters to be delivered should not be greater than the input string. If you want to receive for example 5 characters of a 10-character string, starting from position 7, only the last 4 characters are delivered.
If the output string is longer than the length defined for the output variable in the field Type, only as many characters are copied as the output variable can hold.The system variable sys_bIsCarry is set.
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
if the output string is longer than the length defined for the output variable 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[32]:='A better life. A better world.';
(*sample string*)
iNumberOfCharacters: INT:=15;
(*characters to be delivered*)
iStartPos: INT:=16;
(*position to start copying*)
sResultString: STRING[15]:='';
(*result here: 'A better world.'*)
END_VAR
In this example the input variables (sInputString, iNumberOfCharacters and iStartPos) have been declared. Instead, you may enter the string, the number of characters to be delivered and the start position directly into the function. The string has to be put in inverted commas, both in the POU header and in the function.
Starting from iStartPos (16), iNumberOfCharacters (15) of sInputString is copied to sResultString.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 5 ;
NETWORK_BODY
B(B_F,MID!,,22,0,27,5,,?DIN?DL?DP?C);
B(B_VARIN,,sInputString,20,1,22,3,);
B(B_VARIN,,iNumberOfCharacters,20,2,22,4,);
B(B_VARIN,,iStartPos,20,3,22,5,);
B(B_VAROUT,,sResultString,27,1,29,3,);
L(1,0,1,5);
END_NETWORK_BODY
END_NET_WORK
END_BODY
sResultString:=MID(IN:=sInputString, L:=iNumberOfCharacters, P:=iStartPos);