VBScript Split function usage (string to array function) _vbs

Source: Internet
Author: User
Tags comparison numeric value

VBScript Split function usage

Usage: return array = Split (original string, string to find, split into several arrays)
Returns a zero-based one-dimensional array that contains the specified number of substrings.
Split (expression[, delimiter[, count[, compare]])

For example, a comma (,) splits a string into an array

Copy Code code as follows:

Str= "1,2,3,4"
Strarr=split (str, ",")
For i=0 to UBound (Strarr)
MsgBox Strarr (i)

The Split function syntax has the following parts:

Partial description
Expression is required. A string expression that contains substrings and delimiters. If expression is a zero-length string (""), split returns an empty array, which is an array of elements and data.
Delimiter is optional. string character used to identify the bounds of the substring. If omitted, an empty characters ("") is used as the separator. If delimiter is a zero-length string, the returned array contains only one element, the complete expression string.

Count is optional. The number of substrings to return,-1 indicates that all substrings are returned.
Compare is optional. Numeric value that represents the comparison method to use when distinguishing substrings. See the "Set Values" section for their values.

Settings for parameters
Setting values
Set value of compare parameter:

Constant numerical description
VbUseCompareOption-1 performs the comparison using the setting value in the Option Compare statement.
Vbbinarycompare 0 performs binary comparisons.
vbTextCompare 1 performs a text comparison.
Vbdatabasecompare 2 is for Microsoft Access only. Perform comparisons based on information from your database.

Copy Code code as follows:

Private Sub Command1_Click ()
Dim MyStr as String
MyStr = "1234567123456712345"
Mystrs = Split (mystr, "67")
For each strs in Mystrs
Print STRs
End Sub

Output results: "12345", "12345", "12345"

Copy Code code as follows:

This VB program is to ask 10 students of the average score of the exam results.
' For example 95 85 70 75 80 90 60 65 95 100
' The average score of these 10 people's scores ...
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:\ mean score. Dat" for Output as #1
Print #1, "95 85 70 75 80 90 60 65 95 100"
Close #1
End If
Open "d:\ mean 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)
Debug.Print A (i); " ";
s = s + A (i)
Next I
Debug.Print ", the average score of 10 students is:" & S/10
End Sub
Private Sub Command1_Click ()
Dim astring as String
Dim R () as String ' The array that divides the variable by ', '
Dim RT as String ' final result, replace with newline character ', '
Dim C as Integer ' This is for recycling
astring = "Advanced, intermediate, Low, advanced"
R = Split (astring, ",") ' break down each directory
For C = 0 to UBound (r) ' C begins with 0 to loop to the maximum subscript of the R array
RT = RT & VbCrLf & VbCrLf & R (C) ' Add each element of an array to RT, split by carriage return
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 you write 0 o'clock in parentheses, return the first element in the array, and write in parentheses 1 o'clock returns the second element in the array. So, when you return data in this notation, you must separate the strings with a single space, and the other characters as only one data. Cases:
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 returns only advanced, intermediate, low-level, advanced only as a string, that is, only the value of the split (astring) (0) is returned, and other values result in a subscript out of bounds error. So when you decompose with the following methods, you can only use a single space to split, but not other characters.

Copy Code code as follows:

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

Split command use
Splits the file into segments.
To split a file into multiple files that contain a specified number of rows
Split [-L linecount] [-a suffixlength] [File [Prefix]]
To split a file into multiple files that contain a specified number of bytes
Split-b Number [k | m] [a suffixlength] [File [Prefix]]
The split command reads the specified file and is written on a group of output files in a 1000-line size. The first output file name consists of a combination of the specified prefix (the default value x) and the AA suffix, and the second file name is composed of a combination of prefix and ab suffixes, so that the dictionary follows Preface to ZZ (up to 676 files). The number of letters for the suffix and the number of output name files can be increased with 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/limits.h file).
If you do not specify an input file or if you specify-(minus) The file name, the split command reads the file from the standard input.
Note: the-B and-L flags are mutually exclusive.
-a suffixlength Specifies the number of letters used to form the suffix portion of the output name file. Number of letters determines the number of possible combinations of output file names. The default is two letters.
-B divides the file into bytes specified by the number variable. The K (kilobytes) or M (megabyte) multiplier is added to the end of the number value to separate the files into number*1024 bytes or several segments of number*1,048,576 bytes.
-L LineCount Specifies the number of rows per output file. The default value is 1000 rows.
Exit status
This command returns the following exit values:
The 0 command runs successfully.
>0 an error occurred.

Copy Code code as follows:

1. To split the file into segments of 1000 lines, enter:
Split book
This example splits the book into 1000-line segments named Xaa, Xab, Xac, and so on.
2. To split the file into segments of 50 lines and specify the filename prefix, enter:
Split-l Book Sect
This example splits the book into 50-line 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 a 2*1024 byte segment, 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 divides the book into segments of 5 lines, named Sectaaa, Sectaab, SECTAAC, and so on, until sectzzz (up to 17,576 files).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.