In IEC 61131-3 compatible programming environments, string manipulation have many inbuilt functions. Unfortunately, sometimes the routines are a little bit hard to find. Even though it is possible to do a search through available functions, just searching by name sometimes won't cut it.
Searching around for a substring function with the terms string
, substr
or just sub
returned no relevant results. Fortunately, we live in the
world where the Internet is almost ever present and it is possible to reach
for the knowledge stored there, provided, someone else had already shared
it before.
To get the leftmost N characters, the function LEFT
is used with the
signature:
STRING LEFT(STRING STR, INT SIZE)
Subsequently, to get the rightmost N characters of the string, to for
instance reduce four character year representation to the two characters,
function RIGHT
can be used:
STRING RIGHT(STRING STR, INT SIZE)
To get the actual substring, it is possible to daisy-chain these two
functions, cutting the string from the both sides. There is a however an
even shorter way, called MID
, meaning the function is returning the
middle part of the string, or the substring:
STRING MID(STRING STR, INT LEN, INT POS)
I do not know who was creating these names, but in my mind, a middle is a
point on the line having the same distance from both line's ends, which
clearly is not the case for the MID
function, as it starts at the given
POSition an takes specified LENght of characters. Hopefully I will finally
remember this non-intuitive set of functions now that I shared it.
This is a 66th post of #100daystooffload.