Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

傳回 Long,其中包含字串中的字元數或儲存 變數所需的位元元組數。

語法

Len ( 字串 | varname )

Len 函數語法具有下列自變數:

引數

描述

string

任何有效的 字串運算式。 如果 string 包含 Null,就會傳回 Null。

varname

任何有效的 變數 名稱。 如果 varname 包含 Null,就會傳回 Null。 如果 varname 是 Variant,Len 會將其視為 String,並一律傳回其包含的字元數。

註解

必須指定兩個可能自變數的一個 (和一個) 。 在使用者定義的類型中, Len 會傳回寫入檔案時的大小。

附註:  使用 LenB 函數搭配字串中包含的位元組數據,如 DBCS) 語言 (雙位元組字元集。 LenB 不會傳回字串中的字元數,而是傳回用來代表該字串的位元組數。 使用使用者定義的類型, LenB 會 傳回記憶體內大小,包括元素之間的任何邊框。 如需使用 LenB 的範例程式代碼,請參閱範例主題中的第二個範例。

附註:  使用者定義的數據類型中搭配變數長度字串使用時,Len 可能無法判斷所需的實際儲存位元元組數。

查詢範例

Expression

結果​​

SELECT ProductID, Len (ProductID) AS ProductLen FROM ProductSales;

傳回 [ProductID] 字段的值,以及 ProductLen 欄中這些值的長度。

VBA 範例

附註: 下列範例示範如何在 Visual Basic for Applications (VBA) 模組中使用此函數。 如需使用 VBA 的詳細資訊,請在 [搜尋] 旁的下拉式清單中選取 [開發人員參考],並在 [搜尋] 方塊中輸入一個或多個字詞。

第一個範例使用 Len 傳回字串中的字元數或儲存變數所需的位元組數。 型態...[結束類型] 區塊定義 CustomerRecord 必須前面有關鍵詞 Private,才能出現在類模組中。 在標準模組中, Type 語句可以是 公開的。

Type CustomerRecord    ' Define user-defined type.
ID As Integer ' Place this definition in a
Name As String * 10 ' standard module.
Address As String * 30
End Type
Dim Customer As CustomerRecord ' Declare variables.
Dim MyInt As Integer, MyCur As Currency
Dim MyString, MyLen
MyString = "Hello World" ' Initialize variable.
MyLen = Len(MyInt) ' Returns 2.
MyLen = Len(Customer) ' Returns 42.
MyLen = Len(MyString) ' Returns 11.
MyLen = Len(MyCur) ' Returns 8.

第二個範例使用 LenB 和使用者定義函數 (LenMbcs) 在 ANSI 用來代表字串時傳回字串中的位元組字元數。

Function LenMbcs (ByVal str as String)
LenMbcs = LenB(StrConv(str, vbFromUnicode))
End Function
Dim MyString, MyLen
MyString = "ABc"
' Where "A" and "B" are DBCS and "c" is SBCS.
MyLen = Len(MyString)
' Returns 3 - 3 characters in the string.
MyLen = LenB(MyString)
' Returns 6 - 6 bytes used for Unicode.
MyLen = LenMbcs(MyString)
' Returns 5 - 5 bytes used for ANSI.

字串函數及其使用方法

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×