INSERT block

Short summary

Name

INSERT

→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 IN1 and the return value, hence the following data type is demanded as well: WSTRING. Currently, this data type is not supported for the block.
IEC demands data type ANY_CHARS for the input IN2, hence the following data types are demanded as well:
WSTRING, CHAR, WCHAR . Currently, these data types are not supported for the block.)

Graphical interface

images/download/thumbnails/414782660/INSERT-version-1-modificationdate-1535103270105-api-v2.png

Available since

version 1.31.0 (for logi.CAD 3)

Functionality

The block inserts one string into another string and returns the newly created string.

At input IN1, enter a string (= 1st string). At input IN2, enter another string (= 2nd string). At input P, enter the position within the 1st string. The 2nd string is inserted after this position (the inserting operations begins at P +1).

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

  • In case of P = 0, the 2nd string value is inserted before the 1st string value. In this case, the output ENO is set to value TRUE because the inserting operation begins at 1.

  • If the value for P is > the length of the string value for IN1, the block returns the 1st string, blanks up to and including position P and then the 2nd string. In this case, the output ENO is set to value FALSE.

  • If a negative value is connected to P, the block returns only the characters of the 1st string (i.e. the 2nd string is not inserted). In this case, the output ENO is set to value FALSE as well.

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:

IN1

STRING

1st input value (= string where to insert)

IN2

STRING

2nd input value (= string to insert)

P

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

position in the 1st input value after which to insert the 2nd 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).
The following special case might occur regarding this error case: If the result of the call is assigned to a variable that is used as input parameter for the call of this block as well (see the following example), the output ENO of the embracing →POU is set to value FALSE for this error case (but not the output ENO of the called block). Example: ResultString := INSERT(IN1 := 'a', IN2 := ResultString, P := 1);

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 P is > the length of the string value for IN1 or a negative value 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 : STRING[20];
result2 : STRING[7];
CheckENO1, CheckENO2, CheckENO3, CheckENO4, CheckENO5, CheckENO6 : BOOL;
END_VAR
result1 := INSERT(IN1 := 'astring', IN2 := ' 2nd ', P := 1, ENO => CheckENO1);
(* The variable 'result1' evaluates to <'a 2nd string'>. The variable 'CheckENO1' evaluates to 'TRUE'. *)
result2 := INSERT(IN1 := 'astring', IN2 := ' 2nd ', P := 1, ENO => CheckENO2);
(* The variable 'result2' evaluates to <'a 2nd s'>. The variable 'CheckENO1' evaluates to 'FALSE'. Reason: 'result2' is declared with length '7' and the would-be return value exceeds this length. *)
result3 := INSERT(IN1 := 'astring', IN2 := '2nd ', P := 0, ENO => CheckENO3);
(* The variable 'result3' evaluates to <'2nd astring'>. The variable 'CheckENO3' evaluates to 'TRUE'. *)
result4 := INSERT(IN1 := 'astring', IN2 := '2nd', P := 15, ENO => CheckENO4);
(* The variable 'result4' evaluates to <'astring 2nd'>. The variable 'CheckENO4' evaluates to 'FALSE'. Reason: value for 'P' > the length of string 'astring' *)
result5 := INSERT(IN1 := 'astring', IN2 := '2nd', P := -2, ENO => CheckENO5);
(* The variable 'result5' evaluates to <'astring'>. The variable 'CheckENO5' evaluates to 'FALSE'. Reason: negative value for 'P' *)
result6 := INSERT(IN1 := '$B15 deviation', IN2 := '00', P := 2, ENO => CheckENO6);
(* The variable 'result6' evaluates to <'$B1500 deviation'>. The variable 'CheckENO6' 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.