vbscript Split function usage
Usage: Pass back the array = Split (original string, the string to be found, split into several arrays)
describe
Returns a one-dimensional array with subscripts starting from zero, which contains a specified number of substrings.
grammar
Split(expression[, delimiter[, count[, compare]]])
For example, use comma (,) to split a string into an array
str="1,2,3,4"
strarr=split(str,",")
for i=0 to ubound(strarr)
msgbox strarr(i)
next
The syntax of the Split function has the following parts:
Part Description
expression Required. A string expression containing substrings and separators. If expression is a string of zero length (""), Split returns an empty array, that is, an array without elements and data.
Delimiter optional. A string character used to identify the bounds of a substring. If ignored, use the space character (" ") as the separator. If the delimiter is a string of zero length, the returned array contains only one element, the complete expression string.
count optional. The number of substrings to be returned, -1 means that all substrings are returned.
compare Optional. A numeric value indicates the comparison method used when discriminating substrings. For its value, see the Set Value section.
Parameter settings
Set value
Setting value of compare parameter:
Constant Value Description
vbUseCompareOption -1 Use the set value in the Option Compare statement to perform comparison.
vbBinaryCompare 0 Perform binary comparison.
vbTextCompare 1 Perform text comparison.
vbDatabaseCompare 2 is only for Microsoft Access. Perform comparisons based on information from your database.
Private Sub Command1_Click()
Dim MyStr As String
MyStr = "1234567123456712345"
MyStrs = Split(MyStr, "67")
For Each Strs In MyStrs
Print Strs
Next
End Sub
Output result: "12345", "12345", "12345"
'This VB program is to ask for the average scores of 10 students..
'For example 95 85 70 75 80 90 60 65 95 100
'The average scores of these 10 people...
Private Sub Form_Load()
Dim A$(), i As Long, intB As String, s As Integer
If Dir("d:\Average Score.dat") = vbNullString Then
Open "d:\Average Score.dat" For Output As #1
Print #1, "95 85 70 75 80 90 60 65 95 100"
Close #1
End If
Open "d:\Average Score.dat" For Input As #1
Input #1, intB
Close #1
A = Split(intB, Space(1), -1, 1)
For i = 0 To UBound(A, 1)
A(i); " ";
s = s + A(i)
Next i
", the average score of 10 students is:" & s / 10
End Sub
Private Sub command1_Click()
Dim AString As String
Dim r() As String 'Array of splitting variables according to ","
Dim rt As String 'The final result, replace "," with a newline
Dim C As Integer 'This is used for looping
AString = "Advanced, Intermediate, Low, Advanced"
r = Split(AString, ",") 'Decompose each directory
For C = 0 To UBound(r) 'C starts from 0 to the maximum subscript of r array
rt = rt & vbCrLf & vbCrLf & r(C) 'Add each element of the array to rt and split with carriage Enter
Next C'loop
MsgBox rt 'Output
End Sub
Private Sub Form_Load()
Dim strTextDate As String
strTextDate = "2008-12-1 Monday"
MsgBox Format(Split(strTextDate)(0), "yyyy-mm-dd")
End Sub
When 0 is written in brackets, the first element in the array is returned, and when 1 is written in brackets, the second element in the array is returned. And so on, when returning data in this way, a space must be used to separate the strings, and other characters are only treated as one data. example:
Private Sub Form_Load()
Dim AString As String
AString = "Advanced Intermediate Low Level Advanced"
MsgBox Split(AString)(0)
MsgBox Split(AString)(1)
MsgBox Split(AString)(2)
MsgBox Split(AString)(3)
End Sub
The following only returns Advanced, Intermediate, Low, Advanced. It is only treated as a string, that is, it can only return the value of Split(AString)(0). Other values all produce subscript out-of-bounds errors. Therefore, when decomposing using the following method, you can only use one space to divide it, not other characters.
Private Sub Form_Load()
Dim AString As String
AString = "Advanced, Intermediate, Low, Advanced"
MsgBox Split(AString)(0)
MsgBox Split(AString)(1)
MsgBox Split(AString)(2)
MsgBox Split(AString)(3)
End Sub
Use of split command
Split the file into several segments.
grammar
To split a file into multiple files with a specified number of lines
split [ -l LineCount ] [ -a SuffixLength ] [ File [ Prefix ] ]
To split a file into multiple files containing the specified number of bytes
split -b Number [ k | m ] [ -a SuffixLength ] [ File [ Prefix ] ]
describe
The split command reads the specified file and is cased in 1000 lines on a set of output files. The first output file name consists of a combination of the specified prefix (default x) and aa suffix, and the second file name consists of a combination of the prefix and ab suffix, so as to the zz (up to 676 files) in the dictionary order. The number of letters of the suffix and therefore the number of output names files can be increased by the -a flag.
The Prefix you specify cannot be longer than PATH_MAX - 2 bytes (if the -a flag is specified, it cannot be longer than PATH_MAX - SuffixLength bytes). The PATH_MAX variable specifies the length of the system's maximum pathname (defined in the /usr/include/sys/ file).
If you do not specify the input file or if you specify the - (minus sign) file name, the split command reads the file from standard input.
Logo
Note: The -b and -l flags are mutually exclusive.
-a SuffixLength Specifies the number of letters used to form the suffix part of the output name file. The number of letters determines the number of possible output file name combinations. The default is two letters.
-b Number Split the file into the number of bytes specified by the Number variable. Adding the k (kilobyte) or m (megabyte) multiplier to the end of the Number value makes the file separate into several segments of Number*1024 bytes or Number*1,048,576 bytes respectively.
-l LineCount Specifies the number of lines for each output file. The default value is 1000 rows.
Exit status
This command returns the following exit value:
0 The command runs successfully.
>0 An error occurred.
Example
1. To split the file into 1000-line segments, enter:
split book
This example splits the book into 1000-row segments named xaa, xab, xac, and more.
2. To split the file into 50-line segments and specify a file name prefix, enter:
split -l 50 book sect
This example splits the book into 50-row segments named sectaa, sectab, sectac, and so on.
3. To split the file into 2KB segments, enter:
split -b 2k book
This example splits the book into 2*1024 byte segments named xaa, xab, xac, and so on.
4. To split the file into more than 676 segments, enter:
split -l 5 -a 3 book sect
This example splits the book into 5-line segments, named sectaaa, sectaab, sectaac, etc., until sectzzzz (up to 17,576 files).