Vbscript Split function usage (string-to-Array Function), vbscriptsplit

Source: Internet
Author: User

Vbscript Split function usage (string-to-Array Function), vbscriptsplit

Vbscript Split function usage

Usage: return array = Split (original string, string to be searched, Split into several arrays)
Description
Returns a one-dimensional array with a subscript starting from zero. It contains a specified number of substrings.
Syntax
Split (expression [, delimiter [, count [, compare])

For example, use commas (,) to separate strings into arrays.

Copy codeThe Code is as follows:
Str = "1, 2, 4"
Strarr = split (str ,",")
For I = 0 to ubound (strarr)
Msgbox strarr (I)
Next

The Split function syntax includes 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 with no elements or data.
Optional. String character used to identify the substring boundary. If ignored, the space character ("") is used as the separator. If delimiter is a zero-length string, the returned array contains only one element, that is, the complete expression string.

Optional. The number of substrings to return.-1 indicates that all substrings are returned.
Compare is optional. Numeric value, indicating the comparison method used to identify a substring. For more information about the value, see "set value.

Parameter settings
Set Value
Set the value of the compare parameter:

Constant Value description
VbUseCompareOption-1 is compared with the set value in the Option Compare statement.
VbBinaryCompare 0 performs binary comparison.
VbTextCompare 1.
VbDatabaseCompare 2 is only used for Microsoft Access. Perform comparison based on your database information.

Copy codeThe Code is as follows:
Private Sub commandementclick ()
Dim MyStr As String
MyStr = "1234567123456712345"
MyStrs = Split (MyStr, "67 ")
For Each Strs In MyStrs
Print Strs
Next
End Sub

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

Copy codeThe Code is as follows:
'The vbprogram is used to calculate the average score of 10 students ..
'Example 95 85 70 75 80 90 60 65 95 100
'The average score of the 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)
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 commandementclick ()
Dim AString As String
Dim r () As String 'splits the variables into arrays separated ","
The final result of Dim rt As string' is replaced by a linefeed ","
Dim C As Integer 'is used cyclically.
AString = "advanced, intermediate, low, advanced"
R = Split (AString, ",") 'splits each directory
For C = 0 To UBound (r) 'C loops from 0 To the maximum subscript of the r Array
Rt = rt & vbCrLf & r (C) 'add each element of the array to rt and separate it 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 0 is written in parentheses, the first element in the array is returned. If 1 is written in parentheses, the second element in the array is returned. In this way, when data is returned in this way, a space must be used to separate the string. Other characters are only treated as one data. Example:
Private Sub Form_Load ()
Dim AString As String
AString = "advanced, intermediate, and advanced"
MsgBox Split (AString) (0)
MsgBox Split (AString) (1)
MsgBox Split (AString) (2)
MsgBox Split (AString) (3)
End Sub

Only the values of advanced, intermediate, and low are returned as one string. That is, only the Split (AString) (0) value can be returned. Other values produce subscript out-of-bounds errors. Therefore, only one space can be used to separate data using the following methods, rather than other characters.

Copy codeThe Code is 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

Purpose of the split command
Split the file into several segments.
Syntax
Split a file into multiple files containing the specified number of rows
Split [-l LineCount] [-a SuffixLength] [File [Prefix]
Split a file into multiple files containing the specified number of bytes
Split-B Number [k | m] [-a SuffixLength] [File [Prefix]
Description
The split command reads the specified file, with 1000 rows of case in a group of output files. The first output file name consists of the specified prefix (default value x) and the aa suffix. The second output file name consists of the prefix and AB suffix, in this way, the sequence until zz (up to 676 files ). The number of letters in the suffix and the number of output name files can be added with the-a sign.
The Prefix you specified cannot be longer than PATH_MAX-2 bytes (if the-a flag is specified, it cannot be longer than PATH_MAX-SuffixLength ). The PATH_MAX variable specifies the length of the maximum path name of the system (defined in the/usr/include/sys/limits. h 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 the standard input.
Flag
Note: The-B and-l signs are mutually exclusive.
-A SuffixLength specifies the number of letters used to form the suffix of the output name file. The number of letters determines the number of possible combinations of output file names. The default value is two letters.
-B Number splits the file into the Number of bytes specified by the Number variable. Add the k (kilobytes) or m (MB) multiplier to the end of the Number value to split the file into several segments of Number * 1024 bytes or Number * 1,048,576 bytes, respectively.
-L LineCount indicates the number of lines in each output file. The default value is 1000 rows.
Exit status
This command returns the following exit value:
The 0 command is successfully run.
> 0 error.
Example

Copy codeThe Code is as follows:
1. to split the file into 1000 lines, enter:
Split book
In this example, the book is divided into 1000 rows and named xaa, xab, and xac.
2. to split the file into 50 lines and specify the file name prefix, enter:
Split-l 50 book sect
In this example, the book is divided into 50 lines and named as sectaa, sectac, and so on.
3. to split the file into 2 kb segments, enter:
Split-B 2 k book
In this example, the book is divided into 2*1024 bytes and named xaa, xab, and xac.
4. to divide the file into more than 676 segments, enter:
Split-l 5-a 3 book sect
In this example, the book is divided into five lines and named as sectaaa, sectaab, sectaac, and so on until sectzzz (up to 17,576 files ).


How to Use VBSCRIPT to SPLIT a string into a character array

<% Str = "abcdefg hi! "
Dim array1
Strlen = len (str)
Redim array1 (strlen)
Do while strlen> 0
Array1 (strlen) = right (str, 1)
Str = left (str, strlen-1)
Strlen = len (str)
Loop

For I = 1 to ubound (array1)
Response. write array1 (I) & "<br>"
Next %>

Splitting strings into Arrays Using the VB Function Split ()

No, but you can use the Replace function to Replace "@" with "#" and then use the Split function to Split the string into an array.
For example:
B = Split (Replace (a, "@", "#", vbTextCompare ),"#")

Related Article

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.