.NET Framework Class Library Strings..::.Len Method (Int32) 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)

Syntax
Public Shared Function Len ( _
Expression As Integer _
) As Integer
public static int Len(
int Expression
)
public:
static int Len(
int Expression
)
static member Len :
Expression:int -> int
Parameters- Expression
- Type: System..::.Int32
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 ValueType: System..::.Int32Returns an integer containing either the number of characters in a string or the nominal number of bytes required to store a variable.

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. |
Smart Device Developer NotesThe 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.

Examples
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)

Version Information
.NET FrameworkSupported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileSupported in: 4, 3.5 SP1

Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

See Also
|
.NET Framework クラス ライブラリ Strings..::.Len メソッド (Int32) 文字列内の文字数または変数を格納するために必要な公称バイト数を表す整数を返します。
名前空間:
Microsoft.VisualBasic
アセンブリ:
Microsoft.VisualBasic (Microsoft.VisualBasic.dll 内)

構文
Public Shared Function Len ( _
Expression As Integer _
) As Integer
public static int Len(
int Expression
)
public:
static int Len(
int Expression
)
static member Len :
Expression:int -> int
パラメーター- Expression
- 型: System..::.Int32
任意の有効な String 型の式または変数名。 Expression の型が Object である場合、Len 関数は、その型が FilePut 関数によってファイルに書き込まれるときのサイズを返します。

解説
ユーザー定義の型およびオブジェクト型 (Object) の変数の場合、Len 関数はその型が FilePut 関数によってファイルに書き込まれるときのサイズを返します。 Object に文字列型 (String) が含まれる場合は、文字列の長さを返します。 Object にその他の型が含まれる場合は、オブジェクトが FilePut 関数によってファイルに書き込まれるときのサイズを返します。 VBFixedString 属性をオブジェクト内の文字列フィールドに適用すると、ディスクに書き込むときに文字列のサイズをバイト数で示すことができます。 Len 関数は、Object 変数のサイズを判断するとき VBFixedString 属性を (可能であれば) 使用します。 メモ |
|---|
Len 関数をユーザー定義のデータ型に含まれる可変長文字列に対して実行する場合は、格納に必要な実際のバイトを決定できない場合があります。 |
メモ |
|---|
以前のバージョンの Visual Basic では、LenB 関数は文字数ではなくバイト数を返していました。 これは主に、2 バイト文字セット (DBCS) アプリケーションで文字列を変換するために使用します。 現在 Visual Basic のすべての文字列は Unicode で、LenB はサポートされていません。 |
スマート デバイス開発者のためのメモLen 関数は、String 変数と Object 変数だけをパラメーターとして受け入れます。 Object に文字列型 (String) が含まれる場合は、String の長さを返します。 パラメーターが NULL Object 参照である場合は、ゼロを返します。 Object にその他の型が含まれる場合は、例外がスローされます。

例
Len 関数を使って文字列の文字数を返す例を次に示します。
' Initializes variable.
Dim TestString As String = "Hello World"
' Returns 11.
Dim TestLen As Integer = Len(TestString)

バージョン情報
.NET Frameworkサポート対象: 4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profileサポート対象: 4、3.5 SP1

プラットフォーム
Windows 7, Windows Vista SP1 以降, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core はサポート対象外), Windows Server 2008 R2 (SP1 以降で Server Core をサポート), Windows Server 2003 SP2
.NET Framework では、各プラットフォームのすべてのバージョンはサポートしていません。 サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。

参照
|