The Split function in VBScript returns a zero-based, one-dimensional array containing a specified number of substrings, divided from a larger string based on a delimiter. For example, Split("apple,banana,cherry", ",") returns an array with three elements: “apple”, “banana”, and “cherry”.
What Is the Split Function?
VBScript’s Split function is a built-in string utility that breaks a single string into multiple substrings at each occurrence of a delimiter. It is the inverse of the Join function, which combines an array of substrings into a single string. The function is widely used in classic ASP, Windows scripting (WSH), and older automation scripts to parse comma-separated values (CSV), log files, or any delimited text.
Syntax and Parameters
The full syntax is:
Split(expression, [delimiter], [count], [compare])
Parameters:
- expression (required): The string to be split.
- delimiter (optional): The character or substring used to identify split points. Default is a space (” “). If set to an empty string (“”), the entire string is returned as a single-element array.
- count (optional): The maximum number of substrings to return. If omitted or -1, all substrings are returned. If 0, an empty array is returned.
- compare (optional): A numeric value indicating the comparison method: 0 for binary (case-sensitive) and 1 for text (case-insensitive). Default is 0.
All parameters except expression are optional, but if you want to specify compare, you must specify count as well (or use -1).
How Split Works: Step-by-Step
When you call Split, VBScript scans the input string from left to right, looking for the delimiter. Each time it finds the delimiter, it extracts the substring before it and adds it to the array. After the last delimiter, the remaining text becomes the final element. If the delimiter appears at the beginning or end, empty strings are included.
Example:
Dim str, arr
str = "one,two,,three,"
arr = Split(str, ",")
' arr(0)="one", arr(1)="two", arr(2)="", arr(3)="three", arr(4)=""
The array is zero-based, so the first element is at index 0. Use UBound(arr) to get the highest index; the number of elements is UBound(arr) + 1.
Practical Examples
Here are common uses:
- Parsing CSV lines:
Split(line, ",")gives fields. - Extracting words:
Split(sentence, " ")splits on spaces. - Limiting splits:
Split("a-b-c-d", "-", 2)returns only two elements: “a” and “b-c-d”. - Case-insensitive split:
Split("A,B,a", ",", -1, 1)treats “A” and “a” the same when using text comparison, but since the delimiter is just a comma, comparison doesn’t matter here.
To loop through the results:
Dim i
For i = 0 To UBound(arr)
WScript.Echo arr(i)
Next
Common Pitfalls and Solutions
- Empty string input:
Split("")returns an array with one element: an empty string. If you expect zero elements, checkLen(str) = 0first. - Delimiter not found: The entire string is returned as a single-element array.
- Consecutive delimiters: They produce empty elements. For CSV parsing, you may want to filter these out using a loop or the
Filterfunction. - Performance on large strings:
Splitis fast, but if you need to split into a fixed number of parts, specifyingcountcan save memory. - Using a multi-character delimiter: The delimiter can be a substring, e.g.,
Split("a||b", "||")works.
Split vs. Other String Functions
VBScript also has Mid, Left, Right, and InStr for extracting substrings, but they require manual loop logic to split on multiple delimiters. Split is the only built-in function that directly produces an array from delimited text. The Join function is the direct opposite. For complex parsing, you might combine Replace to normalize delimiters before splitting.
Frequently Asked Questions
Can I split a string into individual characters?
Yes, set the delimiter to an empty string: Split("hello", "") returns an array of five elements, each a single character. This works because VBScript treats an empty delimiter as a split after every character.
What happens if the delimiter is a space and the string has multiple spaces?
Each space is treated as a delimiter, so consecutive spaces create empty elements. For example, Split("a b", " ") returns three elements: “a”, “”, “b”. To collapse multiple spaces, use a loop or replace double spaces first.
How do I get the number of elements in the returned array?
Use UBound(arr) + 1. Note that UBound returns the highest index, so for a three-element array, it returns 2.
Is Split case-sensitive by default?
Yes, the default comparison is binary (case-sensitive). To make the delimiter matching case-insensitive, pass 1 as the compare parameter. This is rarely needed because delimiters are usually symbols like commas or pipes.
Can I split a string into a fixed number of parts and keep the rest in the last element?
Yes, use the count parameter. For instance, Split("a,b,c,d", ",", 3) returns three elements: “a”, “b”, and “c,d”. The remainder of the string (including any delimiters) is placed in the last element.
Does Split work in VBScript inside HTML (client-side)?
Yes, VBScript can be used in classic ASP on the server, but in client-side HTML, VBScript is only supported in Internet Explorer (and now deprecated). For modern browsers, use JavaScript’s split() method instead.
Related
- Split a Column in Snowflake: SQL Methods and Examples
- Java String matches(): How Regex Matching Works
- MS Access: How to Split a Database for Multiple Users
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Excel Percentile Function: Rank, Syntax, and ExamplesExcel has separate functions for percentiles and percentile ranks: use PERCENTILE.INC or PERCENTILE.EXC to find the value at…
- Excel Conditional Formatting If No Formula?Yes, Excel conditional formatting can work without a formula; you can use built-in rules like Highlight Cells Rules,…
- RANK Excel Function: Examples, Ties, and Sort OrderTo rank a value in Excel, use RANK.EQ(number,ref,[order]): for example, with scores in B2:B6, =RANK.EQ(B2,$B$2:$B$6,0) returns B2’s position…
- Microsoft Excel Concatenate Text: Formulas and ExamplesTo concatenate text in Microsoft Excel, join cell contents with the ampersand operator (&), or use CONCAT or…
- Excel COUNTIF Contains Text: Formula & ExamplesTo count cells that contain a specific text string in Excel, use =COUNTIF(range,"*text*"). For example, =COUNTIF(A2:A10,"*apple*") counts every…
- DMAX Function in Excel: Syntax, Examples, and CriteriaExcel’s DMAX function returns the largest numeric value in a selected column of a database-like table, but only…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.