Text Functions in Expressions
Expressions can be used to display calculated text as well as numeric values. For example, you might use the Concat() function to join the value of a tag or the result of a calculation to a sentence.
A few of the string handling functions in VTScada are as follows. See String And Buffer Functions for complete descriptions of these and other functions. (Note: many string functions cannot be used in Steady State, and thus cannot be used in an expression.)
\GetPhrase & \GetParmPhrase |
Note the backslash that begins each of these functions. Given a phrase key, or a parameterized structure (\ParmPhrase) of phrase keys, this will return the matching phrase(s) in the current language. |
Concat(a, b, c…) |
Concatenates any number of sub-strings into one sentence. Works in only one language at a time. Concat("Level of Tank 1: ",[TankLevel_1], "%") Returns (for example): "Level of Tank 1: 30.35552%" Concat(" Viewing Station: ", StationNumber) Example shows how to set the title of a parameterized page, where StationNumber is a text or numeric parameter of that page. |
Format(width, precision, value) |
Turns a numeric value into a text string, having the specified width and precision (number of decimal points). Format(5, 2, [TankLevel_1]) Returns (for example): "30.36" |
Replace(sentence, start, length, find, replace) |
Searches the sentence, beginning at the Start character and continuing for Length characters, looking for every instance of Find and replacing it with Replace. Note that character counting begins with 0. Replace("This is good", 1, 12, "is", "was") Returns: "Thwas was good" (Note that every instance of "is" is replaced. This may have unintended consequences.) |
SubStr(sentence, start, length) |
Returns a substring of Sentence, beginning with the Start character and running for Length characters. Substr("on a Halifax pier", 5, 7) Returns: "Halifax" |
Examples: Text Functions in Expressions
Concat
One of the most frequently used functions is Concat(). This combines several bits of text together into one sentence. If any of the parameters is a number, it will be converted to text for use in the sentence.
Concat() can take any number of parameters. Don't forget to include spaces unless you want to join parameters together into a single word. Parameters may be expressions. Numeric values will be translated into strings.
Concat("This ", "and ", 3/4, " of that.")
returns "This and 0.75 of that."
As of version 12, you should create \ParmPhrase structures rather then use Concat to generate all user-interface text.
StrLen
Returns the length of a text string measured as the number of bytes. This can be useful to know when you need to find some portion of text.
StrLen("Welcome to VTScada")
will return 18.
SubStr
Returns a portion of a text string, starting at a specified number of characters from the left. Be careful: counting starts at zero.
Providing the length (number of characters) to return is optional. If you don't, you'll get everything from the nth character to the end.
This function has the form:
SubStr(String, Start[, Length])
Those square brackets have a meaning: they enclose parameters that are optional. Do not type the square brackets when writing a function in your expressions.
SubStr("Good morning to you.", 5)
Will return, "morning to you."
SubStr("Good morning to you.", 5, 7)
Will return, "morning"
Locate
This function locates a substring within a longer line of text, returning the location of that substring as the number of bytes from the beginning. Counting begins at zero, therefore if the substring is at the beginning of the longer string, 0 is returned. If no match is found, then -1 is returned.
You can specify a start point, which is useful if the substring occurs several times and you want to find all of them. You just have to run the function several times, starting one character to the right of each successive match.
You can also specify whether you want a case sensitive match or not. (Case sensitive is the default.)
Locate("Big haystack with a needle", 0, "needle")
returns 20
Locate("Big haystack with a needle", 0, "Needle")
returns -1
Locate("Big haystack with a needle", 0, "Needle", 1)
returns 20
Practice with text expressions
Typically, someone will want to use a text function to specify a given tag name on the fly within an expression. Something like...
Concat("[AlarmPriority", x, "]\Description")
...to retrieve the description of a given Alarm Priority tag. This doesn't work.
The expression will return the tag name and stop at that point, not continuing on to evaluate that tag's properties.
Fortunately, there's another way to specify the name of a tag. You can use the Scope() function, which takes any expression that evaluates to a tag name. So, for example,
Scope(VTSDB, "AlarmPriority0")\Description
The quotation marks are around the tag name to tell VTScada that this is text, not the name of a variable. If you use a function that returns text (like Concat does), you don't need to wrap extra quotation marks around it. It's text.
Concat("AlarmPriority", x)
Assuming that x is a variable containing a number from 0 to 4, this will return the name of an AlarmPriority tag as text, which is what the LocalScope function needs for a parameter.
However, there's still a problem... What you'll get won't be the description of the tag, it will be the phrase identifier key for the description. You'll need to do a look-up on that key to get the description by calling \GetPhrase(). (Note the backslash in front of \GetPhrase.)
- Create an I/O tag named X using the Discrete data type and with a Scaled Process Data Max set to 4. No I/O device or I/O address will be used.
- Draw X on the Overview page as a Numeric Entry widget.
- Create another I/O tag using the String Calculation data type.
- Give it an expression that returns the description of any specified Alarm Priority tag.
Your expression should use [X], created in step 1, to get the priority number. - Draw the tag on the Overview page using a Draw Text widget, placing it near the Numeric Entry widget.
- Switch to operator mode and put numbers 0 through 4 in the numeric input widget.