Split 函式

傳回以零起始的一維數 ,其中包含指定數目的子字串。

語法

分割 (運算式, [ 分隔符號, [ limit, [ compare ]]])

Split函式語法具有下列命名引數

部分 描述
運算式 此為必要動作。 包含子字串和分隔符號的字串表達式。 如果 expression 是長度為零的字串, (「」) , 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 支援與意見反應