Codesys Split String [portable] ✦ Essential & Simple
This is the most common approach for standard PLC applications. You use a WHILE loop to find the delimiter and extract substrings.
This avoids copying the entire string repeatedly. Only extract with MID when you are ready to store the token. codesys split string
// Main splitting loop WHILE iTokenIdx <= 50 AND LEN(sWork) > 0 DO iPos := FIND(sWork, sDelimiter); This is the most common approach for standard
The code progressively eats away the string from left to right. However, note that modifying sInput inside the loop using DELETE creates a new copy of the string each iteration, which is inefficient for very long strings (>1000 characters). For most PLC tasks (short strings from sensors), this is perfectly acceptable. Only extract with MID when you are ready to store the token
Extract the temperature from "TEMP:23.5 C" .
// Remove the extracted part + delimiter from the beginning sInput := DELETE(sInput, iStart, iPos - iStart + 1); ELSE // No more delimiters – the last part remains sPart := sInput; // Process the last part // ... bRunning := FALSE; // Exit loop END_IF