iCurrentPos := 1; iTokenIdx := 1; WHILE iTokenIdx <= 50 AND iCurrentPos <= LEN(sWork) DO iNextDelim := FIND(sWork, sDelimiter, iCurrentPos); // Hypothetical FIND from position IF iNextDelim = 0 THEN // Last token from iCurrentPos to end atTokens[iTokenIdx] := MID(sWork, iCurrentPos, LEN(sWork) - iCurrentPos + 1); EXIT; ELSE atTokens[iTokenIdx] := MID(sWork, iCurrentPos, iNextDelim - iCurrentPos); iCurrentPos := iNextDelim + iDelimLen; iTokenIdx := iTokenIdx + 1; END_IF END_WHILE
sData := "TEMP:23.5 C"; iColonPos := FIND(sData, ':'); sTempValue := MID(sData, 5, iColonPos + 1); // Returns "23.5 C" rTemp := STRING_TO_REAL(sTempValue); // Returns 23.5 codesys split string
A delimiter can be more than one character, e.g., "<->" . Always use iDelimLen instead of assuming length 1. iCurrentPos := 1; iTokenIdx := 1; WHILE iTokenIdx
FUNCTION_BLOCK FB_StringSplitter VAR_INPUT sSource : STRING(255); // The string to split sDelimiter : STRING(5); // Delimiter (e.g., ",", ";", " ") END_VAR Example Code: )
: Continue until the FIND function returns 0, indicating no more delimiters remain. Example Code:
). Instead, it is typically achieved using standard string manipulation functions or specific library tools. 1. Standard Library Method (Manual Parsing)