Skip to content

Split ​

返回一个从零开始的一维数组,包含指定数量的子字符串。

语法:Split( expression [ , delimiter [ , limit [ , compare ] ] ] )

expression
必需 包含子字符串和分隔符的字符串表达式。如果expression为零长度字符串(""),Split返回空数组,即没有元素和数据的数组。
delimiter
可选 用于标识子字符串边界的字符串字符。如果省略,则假定空格字符(" ")为分隔符。如果delimiter为零长度字符串,则返回包含整个expression字符串的单元素数组。
limit
可选 要返回的子字符串数;-1表示返回所有子字符串。
compare
可选 数值,指示在计算子字符串时使用的比较类型。参见下面的设置。

compare参数可以取以下值:

常量值描述
vbUseCompareOption-1使用Option Compare语句的设置进行比较。
vbBinaryCompare0执行二进制比较。
vbTextCompare1执行文本比较。

示例 ​

本示例展示如何使用Split函数。

vb
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"

' arrSplitStrings1 will be an array from 0 To 8.
' arrSplitStrings1(0) = "Dow " and arrSplitStrings1(1) = " Fonseca ".
' The delimiter did not include spaces, so the spaces in strFull will be
' included in the returned array values.
arrSplitStrings1 = Split(strFull, "-")

' arrSplitStrings2 will be an array from 0 To 8.
' arrSplitStrings2(0) = "Dow" and arrSplitStrings2(1) = "Fonseca".
' The delimiter includes the spaces, so the spaces will not be included
' in the returned array values.
arrSplitStrings2 = Split(strFull, " - ")

' Multiple examples of how to return the value "Kopke" (array position 3).
strSingleString1 = arrSplitStrings2(3)         ' "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.
strSingleString2 = Split(strFull, " - ")(3)    ' "Kopke".

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

另请参阅 ​

twinBASIC及其LOGO版权为作者"韦恩"所有