Export (0) Print
Expand All

Strings.Len Method (String)

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

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

public static int Len(
	string Expression
)

Parameters

Expression
Type: System.String

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.

For more detailed information, see the Visual Basic topic Len Function (Visual Basic).

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.

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)

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft