String Instructions — CONCAT, FIND, MID, INSERT, DELETE

The Structured Text string instructions in rungs.dev join STRINGs, find text inside one, and take part of one out, each as a statement of its own.

A STRING tag holds text: up to 82 characters, with LEN saying how many are in use. These five instructions build text and take it apart, for an operator message, a barcode, or a reply from a serial device.

Each one is a statement of its own that writes its result into its last operand. None of them gives back a value, so PartNumber := MID(Barcode, 3, 4); is refused: write MID(Barcode, 3, 4, PartNumber);. The last operand may also be one the statement reads.

CONCAT(AlarmPrefix, AlarmText, DisplayLine);   (* 'ALARM: ' and 'Low oil' give 'ALARM: Low oil' *)
FIND(Barcode, Separator, 1, SeparatorAt);       (* '-' in 'AB-123' is at 3 *)
MID(Barcode, 3, 4, PartNumber);                 (* 3 characters from character 4: '123' *)
INSERT(Reading, Unit, 5, Shown);                (* ' C' after '25.5': '25.5 C' *)
DELETE(Barcode, 3, 1, Suffix);                  (* 'AB-' removed: '123' *)

Characters are counted from 1. MID and DELETE take the quantity before the start.

Operands

InstructionOperands, in order
CONCATsource A, source B, destination
FINDsource, search, start, result
MIDsource, quantity, start, destination
INSERTsource A, source B, start, destination
DELETEsource, quantity, start, destination

Every text operand is a STRING tag, an element of a STRING array, or a STRING member. A start or a quantity is a DINT, INT or SINT tag, or a whole number typed in, never a REAL: MID(Barcode, 1.0, 4, PartNumber); is refused. FIND's result may be a DINT, INT, SINT or REAL.

Each operand is a tag or a number, never an expression and never text in quotes: FIND(Barcode, Separator, Index + 1, SeparatorAt); is refused. Work the value out into a tag first. Text in quotes goes only in := and in a comparison.

What happens at the edges

InstructionWhenResult
CONCATThe two hold more than 82 characters togetherThe first 82, and a warning
FINDThe search text is not there, or is empty0
FINDThe start is not a character of the source0, and a warning
MIDThe quantity runs past the end, or is negativeThe rest from the start, and a warning
INSERTThe result would be longer than 82 charactersThe destination is left as it was, and a warning
DELETEThe quantity runs past the end, or is negativeEverything from the start removed, and a warning
anyA text operand's LEN is outside 0 to 82Nothing is written (FIND stores 0), and a warning

MID, INSERT and DELETE also leave the destination as it was when the start is not a character they can start at. For INSERT that is 1 to one past the end of source A.

On this page