Export (0) Print
Expand All

Len Function

Returns an integer containing either the number of characters in a string or the number of bytes required to store a variable.

Public Shared Function Len( _
   ByVal Expression As { Boolean | Byte | Char | Double | Integer | Long | 
   Object | Short | Single | String | DateTime | Decimal } _
) As Integer

Parameters

Expression
Any valid String expression or variable name. If Expression is of type Object, the Len function returns the size as it will be written to the file.

Remarks

With user-defined types and Object variables, the Len function returns the size as it will be written to the file. If an Object contains a String, it will return the length of the string. If an Object contains any other type, it will return the size of the object as it will be written to the file.

Note   The Len function may not be able to determine the actual number of storage bytes required when used with variable-length strings in user-defined data types.
The LenB function in earlier versions of Visual Basic returns the number of bytes in a string rather than characters. It is used primarily for converting strings in double-byte character set (DBCS) applications. All Visual Basic .NET strings are in Unicode, and LenB is no longer supported.

Example

This example uses Len to return the number of characters in a string.

Dim MyString As String
Dim MyLen As Integer
MyString = "Hello World"   ' Initializes variable.
MyLen = Len(MyString)   ' Returns 11.

Smart Device Developer Notes

The Len function accepts only String and Object variables as parameters. If an Object contains a String, it will return the length of the String. If a parameter is a NULL Object reference, the function returns zero. If an Object contains any other type, an exception is thrown.

Requirements

Namespace: Microsoft.VisualBasic

Module: Strings

Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)

See Also

Data Type Summary | InStr Function | Programming Element Support Changes Summary

Show:
© 2015 Microsoft