MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › How do you concatenate and extract substrings in structured text (ST)?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

How do you concatenate and extract substrings in structured text (ST)?

Message assembly, alarm-text building, batch-number generation — all string work. ST's string toolkit is short: CONCAT to join, LEFT/RIGHT/MID to extract, LEN for length, DELETE/INSERT to cut and splice — but one trap awaits every newcomer: string length versus buffer size. The sections below give the function table, joining, extraction and the pits.

1. Function quick reference

FunctionDoesExample
LEN(S)String lengthLEN('AB12') = 4
CONCAT(S1,S2)Joins two stringsCONCAT('AB','12') = 'AB12'
LEFT(S,N)Left N charactersLEFT('AB12',2) = 'AB'
RIGHT(S,N)Right N charactersRIGHT('AB12',2) = '12'
MID(S,Pos,N)N characters from position PosMID('AB12',2,2) = 'B1'
DELETE(S,N,Pos)Deletes N from position PosDELETE('AB12',2,2) = 'A2'
INSERT(S1,S2,Pos)Inserts S2 at PosINSERT('A2','B',1) = 'AB2'

Mind the origin convention: most IEC libraries count Pos from 1 (MID('AB12',2,2) starts at the second character). Confirm in your platform's library documentation — a one-position slip skews every extraction.

2. Joining: many parts into one string

CONCAT joins two at a time; longer builds nest or step:

BatchNo := CONCAT(prefix, INT_TO_STRING(seq));  // 'PC-' + 123 → 'PC-123'
AlarmText := CONCAT(station, CONCAT(': ', errorDesc));  // three parts, nested

Engineering advice: beyond three parts, build step by step into temporary strings (one CONCAT per step) — readable and debuggable; a five-deep nested one-liner lives on bracket-matching and dies at the first edit. Numbers become strings through INT_TO_STRING / DINT_TO_STRING conversions — concatenating a raw number is a compile error or mojibake.

3. Extraction: fixed-length frames and variable-length text

SceneMethodPoints
Fixed-length framesMID by fixed offsets per fieldOffset constants + comments = the field table
Variable-length textLEN first, FIND for delimiters, then cutWithout FIND, a small loop finds it
Suffixes / extensionsRIGHT(S, N)Confirm length ≥ N first
Trimming spacesLEN-driven shrink loopOr the platform's TRIM-type function

Variable-length parsing must verify length first: an over-short string makes MID return empty or skew — "verify length, then extract" is the parsing iron law.

4. The big trap: string length versus buffer

TrapMechanismDefence
Default 80 charactersSTRING declared without a length defaults to 80 bytes; CONCAT results beyond it are silently truncatedDeclare with length: s : STRING(255);
Result overflowTwo parts joined exceed the buffer; the tail vanishes without alarmCompute LEN before joining; branch on overlength
Chinese takes two bytesOne CJK character occupies 2 bytes; 80 characters hold only 40 Chinese charactersCount bytes; double the budget for Chinese fields
PLC and HMI lengths differThe HMI variable shorter than the PLC's truncates the displayUnify lengths and write the convention down

"Silent truncation" is the nastiest: no error, function looks fine, only the text's tail is missing — batch numbers mismatch, alarm descriptions lose their endings. The only defence: declare every string with an explicit length, verify length before every join.

5. Common errors

SymptomCauseFix
Joined string only half there80-character buffer truncatedDeclare with explicit length
Extraction skewed one positionPos origin misunderstood (0/1-based)Check the platform docs, test one case
Chinese turns to mojibakeEncoding mismatch (PLC/HMI)Unify encoding, test a Chinese sample
Numbers join as odd charactersNo TO_STRING conversionUse the conversion functions
Parsing occasionally returns emptyExtract before verifying lengthLEN check first

6. Notes

ST string work is not about knowing many functions but two disciplines: length management (declared lengths, verified joins) and origin confirmation (checked once per platform). Lose the functions and the documentation has them; lose the disciplines and string problems return forever as "occasionally missing tails, occasionally skewed fields".

7. References

String types and standard functions follow IEC 61131-3 and the platform library documents (CODESYS/TwinCAT String libraries, TIA CONCAT/S_CONV etc.); origin and default-length conventions follow the platform manuals; Chinese character byte counts follow the encoding (GB2312/UTF-8 two or three bytes) and the HMI documentation.

8. Cautions

⚠️ Three traps to avoid:
① The default 80-character buffer truncating silently — text forever missing its tail;
② Pos origin mismatching the platform docs — every extraction skewed;
③ PLC and HMI string lengths differing — Chinese display loses characters.

Write the string conventions into the project standard at kickoff: lengths, encoding, origin, field table — one agreement, and the whole line stops fighting strings.

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL