Q(uick)BASIC Function: LEN

Quick View

LEN

A string processing function that returns the number of characters in a string or the number of bytes required by a variable

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • LEN(stringexpression$)
  • LEN(variable)
Description/Parameter(s)
stringexpression$ Any string expression.
variable Any nonstring variable.
Example
a$ = "Microsoft QBasic" PRINT LEN(a$)

See also:

Syntax
  • LEN(stringexpression)
  • LEN(variable)
Description/Parameter(s)

In the first form, LEN returns the number of characters in the argument stringexpression. The second syntax returns the number of bytes required by a BASIC variable. This syntax is particularly useful for determining the correct record size of a random-access file.

Example

This example prints the length of a string and the size in bytes of several types of variables.

CLS ' Clear screen TYPE EmpRec EmpName AS STRING * 20 EmpNum AS INTEGER END TYPE DIM A AS INTEGER, B AS LONG, C AS SINGLE, D AS DOUBLE DIM E AS EmpRec PRINT "A string:" LEN("A string.") PRINT "Integer:" LEN(A) PRINT "Long:" LEN(B) PRINT "Single:" LEN(C) PRINT "Double:" LEN(D) PRINT "EmpRec:" LEN(E) END

Sample Output:

A string: 9 Integer: 2 Long: 4 Single: 4 Double: 8 EmpRec: 22
Syntax
  • LEN(stringexpression$)
  • LEN(variable)
Description/Parameter(s)

Usage Notes

  • The first syntax returns the number of characters in stringexpression$.
  • The second syntax returns the number of bytes required by a BASIC variable. This syntax is particularly useful for determining the correct record size of a random-access file.
Example

This example uses the LEN function to print the length of a string and the size in bytes of several types of variables.

CLS 'Clear screen. TYPE EmpRec EmpName AS STRING * 20 EmpNum AS INTEGER END TYPE DIM A AS INTEGER, B AS LONG, C AS SINGLE, D AS DOUBLE DIM E AS EmpRec PRINT "A string:"; LEN("A string.") PRINT "Integer:"; LEN(A) PRINT "Long:"; LEN(B) PRINT "Single:"; LEN(C) PRINT "Double:"; LEN(D) PRINT "EmpRec:"; LEN(E) END

Sample Output:

A string: 9 Integer: 2 Long: 4 Single: 4 Double: 8 EmpRec: 22