Skip to content

LEN

Source: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgLen Last revised: 2024-04-03

Returns the length of an expression or data type.

Syntax

declare function Len ( byref expression as string ) as integer
declare function Len ( byref expression as zstring ) as integer
declare function Len ( byref expression as wstring ) as integer

declare operator Len ( byref expression as datatype ) as datatype

declare function Len ( datatype ) as integer

Usage:

result = Len( expression )
result = Len( DataType )

Parameters

  • expression — An expression of any type.
  • datatype — A DataType.

Return Value

Returns the size of an expression or DataType (including the data fields of a UDT) in bytes.

Description

Len returns the length of an expression or the size of a DataType, in bytes.

  • If expression is of type String, Wstring, or Zstring, the length of the string in characters is returned.
  • If the expression is of a user-defined type, an Operator Len compatible with that data type is called.
  • Otherwise, the size of the expression's data type in bytes is returned.

In the second form (with a DataType argument):

  • If DataType is Zstring or Wstring, the size in bytes of one ASCII or Unicode character is returned.
  • If DataType is String, the size of the string descriptor type is returned.

Note: If there is both a user-defined type and a variable visible with the same name in the current scope, the user-defined type takes precedence. To force Len to use the variable, wrap the argument in parentheses: Len((variable)).

Note: When used with array names, Len returns the length of the array's datatype, not the total number of elements. Prefer Len(Typeof(array)) for clarity.

The Len unary operator can be overloaded with user-defined types.

Examples

vb
Print Len("hello world")   ' returns "11"
Print Len(Integer)         ' returns 4

Type xyz
    a As Integer
    b As Integer
End Type

Print Len(xyz)             ' returns 8

Version

  • Before fbc 1.08.0, Len was not returning the size of the data fields of a UDT.

Dialect Differences

  • Len only allows expressions in the -lang qb dialect.
  • Can be used with built-in types and user-defined types in -lang fb and -lang fblite dialects.

Differences from QB

  • Can be used with built-in types and user-defined types in -lang fb and -lang fblite dialects.

See Also

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft