Export (0) Print
Expand All

Strings.Len Method (UInt32)

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

This API is not CLS-compliant. The CLS-compliant alternative is Len.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

'Declaration
<CLSCompliantAttribute(False)> _
Public Shared Function Len ( _
	Expression As UInteger _
) As Integer

Parameters

Expression
Type: System.UInt32

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.

Return Value

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

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.

NoteNote

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.

NoteNote

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.

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.

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)

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft