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.

傳回 Variant (String),包含字串的指定字元數。

語法

mid ( string, start [, length ] )

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

引數

描述

string

必要。 字串運算式 傳回字元。 如果 string 包含 Null,就會傳回 Null

開始

必要。 長。 要在 字串 中開始選取部分的字元位置。 如果 start 大於 string 中的字元數, Mid 會傳回零長度字串 (“”) 。

length

選擇性。 Variant (Long) 。 要傳回的字元數。 如果省略或文字中的 長度 字元少於 (包括 開始) 的字元,則會傳回從 開始 位置到字元串結尾的所有字元。


註解

若要判斷 string 中的字元數,請使用 Len 函數。

附註: 使用 MidB 函數搭配字串中包含的位元組數據,如雙位元組字元集語言。 自變數會指定位元組數位,而不是指定字元數。 如需使用 MidB 的範例程式代碼,請參閱範例主題中的第二個範例。

查詢範例

Expression

結果​​

SELECT ProductID, Mid (ProductID, 5) AS Expr1 FROM ProductSales;

傳回從字元位置 5 開始算起的 「ProductID」和 ProductID 部分,並在表達式1 欄中顯示結果。

SELECT ProductID, Mid (ProductID,5,4) AS testMid FROM ProductSales;

會傳回從字元位置 5 開始算起的 「ProductID」 和 ProductID 部分,其中包含 4 個字元,並在欄 testMid 中顯示結果。

VBA 範例

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

第一個範例使用 Mid 函數從字串傳回指定的字元數。

Dim MyString, FirstWord, LastWord, MidWords
MyString = "Mid Function Demo" ' Create text string.
FirstWord = Mid(MyString, 1, 3) ' Returns "Mid".
LastWord = Mid(MyString, 14, 4) ' Returns "Demo".
MidWords = Mid(MyString, 5) ' Returns "Function Demo".

第二個範例使用 MidB 和使用者定義的函數 (MidMbcs) 也會從字串傳回字串。 這裡的差異在於輸入字串是 ANSI,而長度是位元組。

Function MidMbcs(ByVal str as String, start, length)
MidMbcs = StrConv(MidB(StrConv(str, vbFromUnicode), _
start, length), vbUnicode)
End Function
Dim MyString
MyString = "AbCdEfG"
' Where "A", "C", "E", and "G" are DBCS and "b", "d",
' and "f" are SBCS.
MyNewString = Mid(MyString, 3, 4)
' Returns ""CdEf"
MyNewString = MidB(MyString, 3, 4)
' Returns ""bC"
MyNewString = MidMbcs(MyString, 3, 4)
' Returns "bCd"

字串函數及其使用方法

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!

×