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"
이 펑션 프로그램 시 사용한 모든 입력과 출력 변수는 POU 헤더에서 선언되었습니다. 모든 프로그래밍 언어에 같은 POU 헤더를 사용합니다.
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
이 예에서 입력 변수(sInputString, iNumberOfCharacters 및 iStartPos)가 선언됩니다. 대신 문자열, 전달할 문자 수 및 펑션에서 직접 시작하는 위치를 입력할 수 있습니다. POU 헤더와 펑션 모두에서 문자열은 따옴표 안에 있을 수 있습니다.
iStartPos(16)부터 시작하는 sInputString의 iNumberOfCharacters(15)는 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);