Function Len() Foundation
Determines the length of a character string or an array.
Len( <cString> | <aArray> ) --> nCount
Len() returns an integer numeric value corresponding to the length of a character string or the number of elements in an array. For an empty array or a null string (""), zero is returned.
Len() is a character function and an array function which determines the length of a character string or an array. If a character string is passed, each character of a character string is counted, including Chr(0). An empty character string (a null string "") has the length zero.
If an array is passed, Len() determines the number of elements in the first dimension of the array. To determine the total number of array elements in a multi-dimensional array, each subarray must be passed to the function Len() and the return values totaled.
// The example shows various results of the function Len().
PROCEDURE Main
? Len("Xbase++") // result: 7
? Len("") // result: 0
? Len(Chr(0)) // result: 1
? Len( SaveScreen() ) // result: 4000
? Len( {1,2,3,4,5} ) // result: 5
? Len( {1,2,{3,4,5}} ) // result: 3
RETURN
// The example shows one of the most frequent uses of Len()
// with an array. It is used to determine the end of a FOR...NEXT
// loop. In the example the field names and contents of a
// data record are displayed.
PROCEDURE Main
LOCAL aStruct, n, nEnd
USE Address NEW
aStruct := DbStruct()
nEnd := Len( aStruct )
FOR n := 1 TO nEnd
? aStruct[n,1], ":", FieldGet(n)
NEXT
USE
RETURN
// In this example the total number of elements in a
// multi-dimensional array is determined.
PROCEDURE Main
? TotalElements( Directory() )
RETURN
FUNCTION TotalElements( aArray )
LOCAL n, nLen, nTotal
nLen := Len( aArray )
nTotal := 0
FOR n := 1 TO nLen
IF Valtype( aArray[n] ) == "A"
nTotal += TotalElements( aArray[n] )
ELSE
nTotal ++
ENDIF
NEXT
RETURN nTotal
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.