Returns the number of characters or delimited substrings in a string.
Examples
In the following example, both $LENGTH functions return 4, the number of characters in the string.
SET roman="test"
WRITE !,$LENGTH(roman)," characters in: ",roman
SET greek=$CHAR(964,949,963,964)
WRITE !,$LENGTH(greek)," characters in: ",greek
In the following example, the first $LENGTH returns 5. This is the length of 74000, the canonical version of the specified number. The second $LENGTH returns 8, the length of the string “+007.4e4”.
WRITE !,$LENGTH(+007.4e4)
WRITE !,$LENGTH("+007.4e4")
In the following example, the first WRITE returns 11 the number of characters in var1 (including, of course, the space character). The second WRITE returns 2, the number of substrings in var1 using the space character as the substring delimiter.
SET var1="HELLO WORLD"
WRITE !,$LENGTH(var1)
WRITE !,$LENGTH(var1," ")
The following example returns 3, the number of substrings within the string, as delimited by the dollar sign ($) character.
SET STR="ABC$DEF$EFG",DELIM="$"
WRITE $LENGTH(STR,DELIM)
If the specified delimiter is not found in the string $LENGTH returns 1, because the only substring is the string itself.
The following example returns a 0 because the string tested is the null string.
SET Nstring = ""
WRITE $LENGTH(Nstring)
The following example shows the values returned when a delimiter or its string is the null string.
SET String = "ABC"
SET Nstring = ""
SET Delim = "$"
SET Ndelim = ""
WRITE !,$LENGTH(String,Delim) ; returns 1
WRITE !,$LENGTH(Nstring,Delim) ; returns 1
WRITE !,$LENGTH(String,Ndelim) ; returns 0
WRITE !,$LENGTH(Nstring,Ndelim) ; returns 0