String handling
- Ivaylo Fiziev
- 5 days ago
- 2 min read

When working with strings, often there is a need of doing string processing. e.g. searching, replacing, extracting substrings. SCL offers a standard set of functions for cases like these. Until now these functions were not supported but since version 2512 they are supported.
With this post I would like showcase the usage of these functions so you have an example. Don't worry. It is simple.
Lets start with a list of them:
LEN - Returns the current length of a string
FIND - Provides the position of a string within another string
LEFT - Provides the first L characters of a string
RIGHT - Provides the last L characters of a string
MID - Provides the middle part of a string
INSERT - Inserts a string, after a specified character index, into another string
DELETE - Deletes several characters, starting from the character at a given position, in a string
REPLACE - Replaces several characters of a string, starting from the character at a given position, with another string
CONCAT - Concatenates two STRING variables together to form one string
Pretty basic stuff but essential when strings are involved.
Lets see how these functions can be used to parse an email address. Imagine you have to extract the user and the domain from it. Here is how it looks:
FUNCTION_BLOCK "MAIN"
VERSION:1.0
VAR_INPUT
email : STRING := 'user@domain.com';
END_VAR
VAR_OUTPUT
user : STRING;
domain : STRING;
END_VAR
VAR_TEMP
atPos : INT;
len : INT;
END_VAR
BEGIN
#len := LEN(#email);
#atPos := FIND(IN1:=#email, IN2:='@');
if #atPos > 0 then
#user := LEFT(IN:=#email, L:=#atPos - 1);
#domain := RIGHT(IN:=#email, L:=#len - #atPos);
else
#user := '';
#domain := '';
end_if;
END_FUNCTION_BLOCK
It works nicely.
The problem is that you cannot wire STRING signals to the input and the outputs. Process Simulate does not support them yet. But imagine if it did and you can do this on a string coming from a PLC or from another source ... it gives you the power to process strings at runtime and respond accordingly. One day it will happen. Until then strings are only for internal use.
Bye!
ความคิดเห็น