Udostępnij za pośrednictwem


Len Function (Visual Basic)

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

Public Shared Function Len( _
   ByVal Expression As { Boolean | Byte | SByte | Char | Double |
   Integer | UInteger | Long | ULong | Object | Short | UShort |
   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 by the FilePut function.

Remarks

With user-defined types and Object variables, the Len function returns the size as it will be written to the file by the FilePut function. 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 by the FilePut function.

The VBFixedString attribute can be applied to string fields in objects to indicate the size of the string, in bytes, when written to disk. The Len function uses the VBFixedString attribute, if available, when determining the size of Object variables.

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.

Note

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 current Visual Basic strings are in Unicode, and LenB is no longer supported.

Example

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

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

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: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Concepts

Programming Element Support Changes Summary

Reference

Data Type Summary (Visual Basic)

FilePut Function

VBFixedStringAttribute Class

String Manipulation Summary

Data Type Summary (Visual Basic)

InStr Function (Visual Basic)