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

The Ladder Logic string instructions in rungs.dev join STRINGs, find text inside one, and take part of one out, for building operator messages and reading codes such as a barcode.

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. Use them to put an operator message together from pieces, to find a separator in a barcode, or to pull a part number out of a longer code.

Each one runs on every scan its rung is true, and does nothing on a false rung. Each one writes its result into its last operand. That operand may also be one it reads: CONCAT(Msg,Suffix,Msg) adds Suffix to the end of Msg.

Characters are counted from 1: in 'hello', h is character 1 and o is character 5.

CONCAT — Join Two STRINGs

CONCAT

Source A

sourceA

Source B

sourceB

Dest

dest

Joins two STRINGs, Source A then Source B, into the destination STRING.

NameType
sourceASTRING
sourceBSTRING
destSTRING

0

ShowAlarm

CONCAT

Source A

AlarmPrefix

Source B

AlarmText

Dest

DisplayLine

With AlarmPrefix holding 'ALARM: ' and AlarmText holding 'Low oil', DisplayLine becomes 'ALARM: Low oil'. If the two hold more than 82 characters together, the result keeps the first 82 and Studio shows a warning.

FIND — Find Text in a STRING

FIND

Source

source

Search

search

Start

start

Result

result

Finds where the Search STRING first appears in the Source STRING, from the Start character on, and stores its position.

NameType
sourceSTRING
searchSTRING
startDINT, INT or SINT
resultDINT, INT, SINT or REAL

0

CodeReady

FIND

Source

Barcode

Search

Separator

Start

1

Result

SeparatorAt

With Barcode holding 'AB-123' and Separator holding '-', SeparatorAt becomes 3. When the text is not there, the result is 0. Upper and lower case are different characters, so searching 'hello' for 'H' finds nothing.

start says where the search begins, and it must be a character the source has: 1 to its LEN. Outside that, the result is 0 and Studio shows a warning. A typed-in start of 0 is refused before the program runs.

MID — Take Part of a STRING

MID

Source

source

Qty

qty

Start

start

Dest

dest

Copies Qty characters of the Source STRING, from the Start character on, into the destination STRING.

NameType
sourceSTRING
qtyDINT, INT or SINT
startDINT, INT or SINT
destSTRING

qty comes before start. MID(Barcode,3,4,PartNumber) takes three characters, starting at character 4:

0

CodeReady

MID

Source

Barcode

Qty

3

Start

4

Dest

PartNumber

With Barcode holding 'AB-123', PartNumber becomes '123'. If qty runs past the end of the source, MID takes the rest and Studio shows a warning. If start is not a character the source has, dest is left as it was.

INSERT — Put Text Into a STRING

INSERT

Source A

sourceA

Source B

sourceB

Start

start

Dest

dest

Puts the Source B STRING into the Source A STRING at the Start character and stores the result in the destination STRING.

NameType
sourceASTRING
sourceBSTRING
startDINT, INT or SINT
destSTRING

0

AddUnit

INSERT

Source A

Reading

Source B

Unit

Start

5

Dest

Shown

With Reading holding '25.5' and Unit holding ' C', Shown becomes '25.5 C'. The text goes in before the start character, so start may be 1 to one past the end of sourceA, and one past the end adds it after the last character. If the result would be longer than 82 characters, dest is left as it was and Studio shows a warning.

DELETE — Take Text Out of a STRING

DELETE

Source

source

Qty

qty

Start

start

Dest

dest

Removes Qty characters of the Source STRING, from the Start character on, and stores the rest in the destination STRING.

NameType
sourceSTRING
qtyDINT, INT or SINT
startDINT, INT or SINT
destSTRING

Like MID, qty comes before start:

0

CodeReady

DELETE

Source

Barcode

Qty

3

Start

1

Dest

Suffix

With Barcode holding 'AB-123', Suffix becomes '123': the first three characters are removed. If qty runs past the end, everything from start on is removed and Studio shows a warning.

A STRING with a broken LEN

LEN can be set on its own, to a number outside 0 to 82. A string instruction never reads such a STRING. It leaves dest as it was, or stores 0 for FIND, and Studio shows a warning that names the STRING.

Common Mistakes

  • Putting text in quotes into a string instruction, such as CONCAT(Msg,' C',Msg). Put the text in a STRING tag and use the tag. Text in quotes goes only in MOVE and the compare instructions.
  • Swapping qty and start in MID and DELETE. qty comes first.
  • Typing a qty or a start with a decimal point, such as 1.0. They take whole numbers only, so type 1.
  • Counting characters from 0. The first character is 1.

On this page