Syntax
|
Len
(expression)
|
Description
|
Returns the number of characters in expression or the number of bytes required to store the specified variable.
|
Comments
|
If expression evaluates to a string, then
Len
returns the number of characters in a given string or 0 if the string is empty. When used with a
Variant
variable, the length of the variant when converted to a String is returned. If expression is a
Null
, then
Len
returns a
Null
variant.
If used with a non-String or non-
Variant
variable, the function returns the number of bytes occupied by that data element.
When used with user-defined data types, the function returns the combined size of each member within the structure. Since variable-length strings are stored elsewhere, the size of each variable-length string within a structure is 2 bytes.
The following table describes the sizes of the individual data elements:
|
|
Data Element
|
Size
|
|
Integer
|
2 bytes
|
|
Long
|
4 bytes
|
|
Float
|
4 bytes
|
|
Double
|
8 bytes.
|
|
Currency
|
8 bytes
|
|
String
(variable-length)
|
Number of characters in the string.
|
|
String
(fixed-length)
|
The length of the string as it appears in the string's declaration.
|
|
Objects
|
0 bytes. Both data object variables and variables of type
Object
are always returned as 0 size.
|
|
User-defined type
|
Combined size of each structure member.
Variable-length strings within structures require 2 bytes of storage.
Arrays within structures are fixed in their dimensions. The elements for fixed arrays are stored within the structure and therefore require the number of bytes for each array element multiplied by the size of each array dimension:
element_size * dimension1 * dimension2...
|
|
The
Len
function always returns 0 with object variables or any data object variable.
|
Example
|
Const crlf = Chr$(13) + Chr$(10)
Sub Main()
'This example shows the Len function used in a routine to change
'uppercase names to lowercase with an uppercase first letter.
lname$ = "WILLIAMS"
fl$ = Left(lname$,1)
ln% = Len(lname$)
rest$ = Mid(lname$,2,ln%)
nname$ = fl$ & LCase(rest$)
MsgBox "The proper case for " & lname$ & " is " & nname$ & "."
|
|
'This example returns a table of lengths for standard numeric types.
Dim lns(4)
a% = 100 : b& = 200 : c! = 200.22 : d# = 300.22
lns(1) = Len(a%)
lns(2) = Len(b&)
lns(3) = Len(c!)
lns(4) = Len(d#)
msg1 = "Lengths (in bytes) of standard types:" & crlf & crlf
msg1 = msg1 & "Integer: " & lns(1) & crlf
msg1 = msg1 & "Long: " & lns(2) & crlf
msg1 = msg1 & "Single: " & lns(3) & crlf
msg1 = msg1 & "Double: " & lns(4) & crlf
MsgBox msg1
End Sub
|
See Also
|
InStr (function)
|