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.
| Name | Type |
|---|---|
sourceA | STRING |
sourceB | STRING |
dest | STRING |
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.
| Name | Type |
|---|---|
source | STRING |
search | STRING |
start | DINT, INT or SINT |
result | DINT, 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.
| Name | Type |
|---|---|
source | STRING |
qty | DINT, INT or SINT |
start | DINT, INT or SINT |
dest | STRING |
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.
| Name | Type |
|---|---|
sourceA | STRING |
sourceB | STRING |
start | DINT, INT or SINT |
dest | STRING |
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.
| Name | Type |
|---|---|
source | STRING |
qty | DINT, INT or SINT |
start | DINT, INT or SINT |
dest | STRING |
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 aSTRINGtag and use the tag. Text in quotes goes only inMOVEand the compare instructions. - Swapping
qtyandstartinMIDandDELETE.qtycomes first. - Typing a
qtyor astartwith a decimal point, such as1.0. They take whole numbers only, so type1. - Counting characters from
0. The first character is1.
Related
- MOVE — copy text into a STRING
- Compare — compare two STRINGs
- String conversion — numbers to text and back, and letter case
- String instructions in Structured Text
FAL and FSC — Loop Over an Array
FAL and FSC in rungs.dev run one expression once per array element. FAL stores each answer; FSC stops at the first element that passes. Use them to scale a batch or find a value.
String Conversion — DTOS, STOD, RTOS, STOR, UPPER, LOWER
The Ladder Logic conversion instructions in rungs.dev turn numbers into text and text into numbers, and change the case of letters, for displays, barcodes and serial replies.