Name
Len, LenB Functions
Syntax
Len(string|varname) LenB(string|varname)
stringUse: Required
Data Type: String
A valid string literal or string expression.
varnameUse: Required
Data Type: Any except Object
A valid variable name.
Return Value
Long.
Description
Len returns the number of characters in a string or variable. LenB returns the number of bytes required to store a string in memory.
Rules at a Glance
stringandvarnameare mutually exclusive; that is, you must specify eitherstringorvarname, but not both.If either
stringorvarnameisNull, Len and LenB returnNull.You can’t use Len or LenB with an object variable.
If
varnameis an array, you must also specify a valid subscript. In other words, Len andLenBcan’t determine the total number of elements in or the total size of an array. To determine the size of an array, use the LBound and UBound functions.
Programming Tips and Gotchas
Nonstring data is treated the same as strings when passed to the Len and LenB functions. For example, in the code:
Dim number number = 100 WScript.Echo Len(number)
the Len function returns 3, since that is the number of characters in the value of
number.LenB is intended to work with string data, and returns the number of bytes required to store that string. If a nonstring data type is passed to the LenB function, its value is first converted to a string before its length is determined.
VBA/VBScript Differences
Although the Len and LenB functions handle strings identically in VBA and VBScript, they handle non-string ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access