Returns the length of a string measured in characters, or in "pieces" separated by a delimiter specified by one of its arguments.
The format for the $LENGTH function is:
$L[ENGTH](expr1[,expr2])
The first expression specifies the string that $LENGTH() "measures".
The optional second expression specifies the delimiter that defines the measure; if this argument is missing, $LENGTH() returns the number of characters in the string.
If the second argument is present and not an empty string, $LENGTH returns one more than the count of the number of occurrences of the second string in the first string; if the second argument is an empty string, the M standard specifies that $LENGTH() returns a zero (0).
$LENGTH() provides a tool for determining the lengths of strings in two ways, characters and pieces. The two argument $LENGTH() returns the number of existing pieces, while the one argument returns the number of characters.
For a process started in UTF-8 mode, $LENGTH() interprets the string argument(s) as UTF-8 encoded. With VIEW "BADCHAR" enabled, $LENGTH() produces a run-time error when it encounters a malformed character.
$ZLENGTH() is the parallel function of $LENGTH(). Irrespective of the setting of VIEW "BADCHAR" and $ZCHSET, $ZLENGTH() interpets string arguments as a sequence of bytes (rather than characters) and can perform all byte-oriented $LENGTH() operations. For more information, refer to “$ZLength()”.
Example:
GTM>Write $length("KINGSTON") 8 GTM>
This uses $LENGTH() to WRITE the length in characters of the string "KINGSTON".
Example:
GTM>set x="Smith/John/M/124 Main Street/Ourtown/KA/USA" GTM>write $length(x,"/") 7 GTM>
This uses $LENGTH() to WRITE the number of pieces in a string, as delimited by /.
Example:
GTM>write $length("/2/3/","/") 4 GTM>
This also uses $LENGTH() to WRITE the number of pieces in a string, as delimited by /. Notice that GT.M. adds one count to the count of delimiters (in this case 3), to get the number of pieces in the string (displays 4).