回傳一個以零為基礎、一維 的陣列 ,包含指定數量的子字串。
語法
分割 (表達式,[ 分隔符,[ 限制,[ 比較 ]]])
Split 函數語法具有以下命名參數:
| 部分 | 描述 |
|---|---|
| 運算式 | 此為必要動作。 包含子字串與分隔符的字串表達式。 若 表達 式為零長度字串 (“” ) ,Split 會回傳一個空陣列,也就是沒有元素與資料的陣列。 |
| 分隔符 | 選用。 字串字元用於識別子字串的限制。 若省略,空格字元 (“ ”) 即為分隔符。 若分 隔符 為零長度字串,則回傳包含整個 表達 式字串的單元素陣列。 |
| 限 | 選用。 要回傳的子字串數量;-1 表示所有子字串都回傳。 |
| compare | 選用。 數值,指出當評估子字串時要使用的比較種類。 請參閱設定一節中的值。 |
設定
compare 引數可具有以下的值:
| 常數 | 值 | 描述 |
|---|---|---|
| vbUseCompareOption | -1 | 使用 Option Compare 陳述式的設定來執行比較。 |
| vbBinaryCompare | 0 | 執行二進位比較。 |
| vbTextCompare | 1 | 執行文字比較。 |
| vbDatabaseCompare | 2 | 僅限 Microsoft Access。 根據資料庫中的資訊執行比較。 |
範例
這個範例展示了如何使用 Split 函數。
Dim strFull As String
Dim arrSplitStrings1() As String
Dim arrSplitStrings2() As String
Dim strSingleString1 As String
Dim strSingleString2 As String
Dim strSingleString3 As String
Dim i As Long
strFull = "Dow - Fonseca - Graham - Kopke - Noval - Offley - Sandeman - Taylor - Warre" ' String that will be used.
arrSplitStrings1 = Split(strFull, "-") ' arrSplitStrings1 will be an array from 0 To 8.
' arrSplitStrings1(0) = "Dow " and arrSplitStrings1(1) = " Fonesca ".
' The delimiter did not include spaces, so the spaces in strFull will be included in the returned array values.
arrSplitStrings2 = Split(strFull, " - ") ' arrSplitStrings2 will be an array from 0 To 8.
' arrSplitStrings2(0) = "Dow" and arrSplitStrings2(1) = "Fonesca".
' The delimiter includes the spaces, so the spaces will not be included in the returned array values.
'Multiple examples of how to return the value "Kopke" (array position 3).
strSingleString1 = arrSplitStrings2(3) ' strSingleString1 = "Kopke".
strSingleString2 = Split(strFull, " - ")(3) ' strSingleString2 = "Kopke".
' This syntax can be used if the entire array is not needed, and the position in the returned array for the desired value is known.
For i = LBound(arrSplitStrings2, 1) To UBound(arrSplitStrings2, 1)
If InStr(1, arrSplitStrings2(i), "Kopke", vbTextCompare) > 0 Then
strSingleString3 = arrSplitStrings2(i)
Exit For
End If
Next i
另請參閱
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。