MID block

Short summary

Name

MID

→POU type

→function

Category

IEC-block, String, block with internal error diagnostic

Conform to →IEC-standard

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/error.svg currently restricted
(IEC demands data type ANY_STRING for the input IN and the return value, hence the following data type is demanded as well: WSTRING. Currently, this data type is not supported for the block.)

Graphical interface

images/download/thumbnails/414782670/MID-version-1-modificationdate-1535103330922-api-v2.png

Available since

version 1.32.0 (for logi.CAD 3)

Functionality

The block returns a partial string of another string.

At input IN, enter a string. At input L, enter the number of characters to return (= the length of the part to return). At input P, enter the start position in the string.

Special cases (see under "Example for usage within ST-editor" ) :

  • In case of L = 0, the block returns an empty string ''. In this case, the output ENO is set to value TRUE.

  • If a negative value is connected to L, the corresponding characters before the position P are deleted in the string. In this case, the output ENO is set to value FALSE. If the block would access characters outside of the visible range (in front of position 1), the block returns an empty string ''.

  • If the value 0 or a negative value is connected to P, the block does not return any characters. In this case, the output ENO is set to value FALSE as well.

  • If the value for L and/or P is > the length of the string value for IN , the block does not return any characters or just the characters that are within the visible range . In this case, the output ENO is set to value FALSE .

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/information.svg logi.CAD 3 determines the character positions within a string as follows: 1, 2, ..., n. 1 corresponds to the leftmost character position and n to the length of the string.
Three-character combination of the dollar sign ($) followed by two hexadecimal digits are evaluated as single character. Example: The string '$B15' (corresponds to '±5') consists of 2 characters. See →character string literal for more examples for those combinations.

Inputs, return value


Identifier

→Data type

Description

Inputs:

IN

STRING

input value

L

USINT, UINT, UDINT, ULINT, SINT, INT, DINT or LINT(corresponds to →generic data type ANY_INT)

number of characters to return

P

USINT, UINT, UDINT, ULINT, SINT, INT, DINT or LINT(corresponds to →generic data type ANY_INT)

start position within input value

Return value:

STRING


Input EN and output ENO are available when →calling the block. See "Execution control: EN, ENO" for information on input EN and output ENO.

See:

Internal error diagnostic for block

The block checks the would-be return value.

If the return value cannot be mapped in the available memory (the return value is too large), the output ENO of the block is set to value FALSE (or an equivalent). When you are using nested blocks with STRING values, observe that the memory for STRING values is restricted (see "In case of nested string blocks: How can the return value be entirely mapped ?" for details and examples).
Moreover, the block checks the connected values whether a non-existent character position in a string is accessed. This is the case, if the value for L or P is > the length of the string value for IN, a negative value is connected to L or P or value 0 is connected to P. The output ENO of the block is set to value FALSE (or an equivalent) in both these cases.

Example for usage within ST-editor

PROGRAM Test
VAR
result1, result3, result4, result5, result6, result7, result8 : STRING[8];
result2 : STRING[2];
CheckENO1, CheckENO2, CheckENO3, CheckENO4, CheckENO5, CheckENO6, CheckENO7, CheckENO8 : BOOL;
END_VAR
result1 := MID(IN := 'astring', L := 3, P := 4, ENO => CheckENO1);
(* The variable 'result1' evaluates to <'rin'>. The variable 'CheckENO1' evaluates to 'TRUE'. *)
result2 := MID(IN := 'astring', L := 3, P := 4, ENO => CheckENO2);
(* The variable 'result2' evaluates to <'ri'>. The variable 'CheckENO2' evaluates to 'FALSE'. Reason: 'result2' is declared with length '2' and the would-be return value exceeds this length. *)
result3 := MID(IN := 'astring', L := 0, P := 4, ENO => CheckENO3);
(* The variable 'result3' evaluates to <''>, i.e an empty string. The variable 'CheckENO3' evaluates to 'TRUE'. Reason: value '0' for 'L' *)
result4 := MID(IN := 'astring', L := -2, P := 4, ENO => CheckENO4);
(* The variable 'result4' evaluates to <'st'>. The variable 'CheckENO4' evaluates to 'FALSE'. Reason: negative value for 'L' *)
result5 := MID(IN := 'astring', L := -2, P := 1, ENO => CheckENO5);
(* The variable 'result5' evaluates to <''>, i.e an empty string. The variable 'CheckENO5' evaluates to 'FALSE'. Reason: negative value for 'L' *)
(* None of the characters are returned because characters outside the visible range would be accessed. *)
result6 := MID(IN := 'astring', L := 3, P := -1, ENO => CheckENO6);
(* The variable 'result6' evaluates to <''>, i.e an empty string. The variable 'CheckENO6' evaluates to 'FALSE'. Reason: negative value for 'L' *)
result7 := MID(IN := 'astring', L := 13, P := 2, ENO => CheckENO7);
(* The variable 'result7' evaluates to <'string'>, i.e an empty string. The variable 'CheckENO7' evaluates to 'FALSE'. Reason: value for 'L' > the length of string 'astring' *)
result8 := MID(IN := 'deviation: $B15', L := 2, P := 12, ENO => CheckENO8);
(* The variable 'result8' evaluates to <'$B15'>, i.e an empty string. The variable 'CheckENO8' evaluates to 'TRUE'. *)
END_PROGRAM

When creating your application within the ST-editor, enter a call of a block by typing the text as requested by the syntax or use Content Assist.