String functions

LEN — string length

LEN(IN: STRING): INT — number of characters in IN. Empty string returns 0.

iN := LEN('hello');           (* → 5 *)
iN := LEN('');                (* → 0 *)
iN := LEN(sUserInput);

LEFT, RIGHT, MID — substring extraction

sPrefix := LEFT(IN := 'forgeiec', L := 5);   (* → 'forge' *)
sSuffix := RIGHT(IN := 'forgeiec', L := 3);  (* → 'iec' *)
sMiddle := MID(IN := 'forgeiec', L := 3, P := 4);  (* → 'gei' *)
FunctionSignatureNotes
LEFT(IN: STRING, L: INT): STRINGFirst L chars
RIGHT(IN: STRING, L: INT): STRINGLast L chars
MID(IN: STRING, L: INT, P: INT): STRINGL chars starting at position P (1-based)

Position counting in MID is 1-based per IEC convention, not 0-based. The first character is P := 1.

CONCAT — concatenation

CONCAT(IN1: STRING, IN2: STRING, ...): STRING — joins all arguments end-to-end. Variadic up to ~32 args in matiec.

sFull := CONCAT('Hello, ', sName, '! Welcome.');

In ST you can also use the + operator on strings if the implementation supports it; matiec accepts both + and CONCAT for binary case but only CONCAT for variadic.

INSERT — splice into a string

INSERT(IN1: STRING, IN2: STRING, P: INT): STRING — returns IN1 with IN2 inserted after position P.

sNew := INSERT(IN1 := 'hello world',
               IN2 := 'beautiful ',
               P := 6);
(* → 'hello beautiful world' *)

DELETE — remove a substring

DELETE(IN: STRING, L: INT, P: INT): STRING — returns IN with L characters starting at position P removed.

sStripped := DELETE(IN := 'hello world',
                    L := 5,
                    P := 7);
(* → 'hello ' *)

REPLACE — substitute a substring

REPLACE(IN1: STRING, IN2: STRING, L: INT, P: INT): STRING — in IN1, replace L characters starting at position P with IN2.

sFixed := REPLACE(IN1 := 'hello world',
                  IN2 := 'matiec',
                  L := 5,
                  P := 7);
(* → 'hello matiec' *)

FIND — locate a substring

FIND(IN1: STRING, IN2: STRING): INT — returns the position (1-based) of the first occurrence of IN2 in IN1, or 0 if IN2 is not found.

iPos := FIND(IN1 := 'hello world', IN2 := 'world');  (* → 7 *)
iPos := FIND(IN1 := 'hello',       IN2 := 'xyz');    (* → 0 *)

WSTRING variants

For wide-character strings (WSTRING, 16-bit per char), matiec provides variants with the same names: LEN_WSTRING, LEFT_WSTRING, etc. Use them when the source data needs to preserve non-ASCII characters across the conversion. Most ForgeIEC projects work in STRING (8-bit) and only switch to WSTRING for HMI text exchange.

IEC reference

IEC 61131-3 third edition (2013), Annex F.2.5 — “Character string functions” Table F.10.

matiec conformance

All standard string functions implemented. Two practical points:

  • 1-based positions in MID/INSERT/DELETE/REPLACE/FIND per IEC convention. Off-by-one bugs are common when porting from C / Python.
  • Silent truncation when assigning a longer string to a shorter target. Use LEN + range-check up front when input size matters.