Use Split() to divide a text string at a character such as a comma, space or pipe (|). The result is a zero-based VBScript array, so the first value is parts(0).
<%
Option Explicit
Dim parts, item
parts = Split("red,green,blue", ",")
For Each item In parts
Response.Write Server.HTMLEncode(item) & "<br>"
Next
%>
Output:
red
green
blue
The example separates the text at each comma and prints each array item on a new line. A Join() operation performs the reverse task: combining array values into a string.
Split(expression[, delimiter[, count[, compare]]])
The expression is the string to divide. The remaining arguments are optional:
| Argument | Purpose | Example |
|---|---|---|
expression | Text to split (required) | "one,two" |
delimiter | Text that marks each split; a single space is the default | "," |
count | Maximum number of parts; -1 returns all parts | 3 |
compare | How the delimiter is matched: vbBinaryCompare or vbTextCompare | vbTextCompare |
For the compare argument, include the earlier optional arguments too; for example, use Split(text, "x", -1, vbTextCompare).
Dim words
words = Split("Welcome to plus2net", " ")
Response.Write words(0) & " / " & words(1) & " / " & words(2)
Output: Welcome / to / plus2net. If the delimiter argument is omitted, VBScript uses a single space.
Dim details
details = Split("Name|Class|Mark", "|")
Response.Write details(1)
Output: Class. The pipe is just another delimiter; it does not require special escaping.
Dim sections
sections = Split("HTML :: CSS :: ASP", " :: ")
Response.Write sections(0) & " - " & sections(2)
Output: HTML - ASP. The delimiter can be a string of multiple characters, not just one character. The complete delimiter sequence must match.
The third argument specifies the maximum number of array elements. With a limit of 3, the first two delimiters are used and the remainder stays in the third element.
<%
Option Explicit
Dim myString, myArray
myString = "Welcome to plus2net, learn web programming and design"
myArray = Split(myString, " ", 3)
Response.Write Server.HTMLEncode(myArray(0)) & "<br>"
Response.Write Server.HTMLEncode(myArray(1)) & "<br>"
Response.Write Server.HTMLEncode(myArray(2))
%>
Output:
Welcome
to
plus2net, learn web programming and design
Use -1 for an unlimited split (the default). A limit of 1 keeps the entire original string in one element, while a limit of 0 returns an empty array.
After splitting, use parentheses for indexes: myArray(0), myArray(1), and so on. VBScript does not use the square-bracket syntax myArray[0] for array indexing. Learn more about VBScript array declarations and array bounds.
<%
Option Explicit
Dim myString, myArray, item
myString = "Welcome to plus2net, learn web programming and design"
myArray = Split(myString, " ")
For Each item In myArray
Response.Write Server.HTMLEncode(item) & "<br>"
Next
%>
You can also use LBound() and UBound() when you need each element’s numeric index:
<%
Dim fruits, i
fruits = Split("apple,banana,mango", ",")
For i = LBound(fruits) To UBound(fruits)
Response.Write i & " = " & Server.HTMLEncode(fruits(i)) & "<br>"
Next
%>
Output: 0 = apple, 1 = banana, 2 = mango, each on a separate line. Use For Each when you only need values, not indexes.
By default, Split() uses binary matching for the delimiter. Specify vbTextCompare to split at both uppercase and lowercase versions of a letter.
<%
Dim textValue, binaryParts, textParts
textValue = "redXgreenxblue"
binaryParts = Split(textValue, "x", -1, vbBinaryCompare)
textParts = Split(textValue, "x", -1, vbTextCompare)
Response.Write "Binary: " & UBound(binaryParts) + 1 & " parts<br>"
Response.Write "Text: " & UBound(textParts) + 1 & " parts"
%>
Output: Binary: 2 parts and Text: 3 parts. A binary comparison matches the lowercase x only, while a text comparison also matches uppercase X.
Consecutive delimiters create empty values. For example, Split("apple,,mango", ",") produces three elements; the middle element is an empty string. To skip empty entries, trim each value and check its length.
<%
Dim inputText, values, value
inputText = "apple, ,banana,, mango"
values = Split(inputText, ",")
For Each value In values
value = Trim(value)
If Len(value) > 0 Then
Response.Write Server.HTMLEncode(value) & "<br>"
End If
Next
%>
Output: apple, banana, mango on separate lines. This is useful for simple delimited lists, but it is not a complete CSV parser when fields contain quoted commas or embedded line breaks. See Trim() for removing outer whitespace.
The server variable SCRIPT_NAME normally contains the virtual path of the ASP script. Split that path by / and take the last array value. This complements the Classic ASP file handling tutorial.
<%
Dim pathValue, pathParts, fileName
pathValue = Request.ServerVariables("SCRIPT_NAME")
pathParts = Split(pathValue, "/")
fileName = pathParts(UBound(pathParts))
Response.Write Server.HTMLEncode(fileName)
%>
For an executing ASP script at /scripts/example.asp, the output would be example.asp. If you only need the last path segment, this is a simple use of splitting.
A server-side page can split a user-submitted list from Request.Form. The example below reads a field named topics, trims the values, and HTML-encodes each one for display. See Classic ASP forms and the Request object.
<%
Dim topicsInput, topicsArray, topic
topicsInput = Trim(Request.Form("topics"))
If Len(topicsInput) > 0 Then
topicsArray = Split(topicsInput, ",")
For Each topic In topicsArray
topic = Trim(topic)
If Len(topic) > 0 Then
Response.Write Server.HTMLEncode(topic) & "<br>"
End If
Next
End If
%>
For a submitted value of HTML, ASP, SQL, the page displays HTML, ASP, and SQL separately. In a production form, also validate length and permitted values before storing or acting on the input.
Dim tags, resultText
tags = Split("ASP,SQL,HTML", ",")
resultText = Join(tags, " | ")
Response.Write Server.HTMLEncode(resultText)
Output: ASP | SQL | HTML. Continue with Join() in VBScript and Filter() for array values.
Splitting key/value text with a limit of 2 is useful when a value itself may include the separator. Without the limit, the extra separator would create additional array elements.
<%
Dim settingLine, pair
settingLine = "theme=dark=contrast"
pair = Split(settingLine, "=", 2)
Response.Write "Key: " & Server.HTMLEncode(pair(0)) & "<br>"
Response.Write "Value: " & Server.HTMLEncode(pair(1))
%>
Output: Key: theme and Value: dark=contrast. The count argument retains the remaining text after the first matching delimiter.
Use vbCrLf to split text containing Windows-style CRLF line breaks. A string using only line-feed (vbLf) separators requires a different delimiter; Split() does not automatically normalize line endings.
<%
Dim notes, rows, row
notes = "First line" & vbCrLf & "Second line"
rows = Split(notes, vbCrLf)
For Each row In rows
Response.Write Server.HTMLEncode(row) & "<br>"
Next
%>
This approach is appropriate when you control the line-ending format. For user-uploaded files, check the actual newline convention before choosing the delimiter.
For a nonempty source string, the number of parts is UBound(parts) - LBound(parts) + 1. Check for empty input before relying on array indexes or bounds.
<%
Dim listText, listParts
listText = "red,green,blue"
If Len(listText) = 0 Then
Response.Write "0 items"
Else
listParts = Split(listText, ",")
Response.Write (UBound(listParts) - LBound(listParts) + 1) & " items"
End If
%>
Output: 3 items. Empty entries created by adjacent commas still count as array elements; filter them first if you need the number of nonempty entries.
| Input or issue | What happens / what to do |
|---|---|
Split("", ",") | Returns an empty array. Avoid assuming element (0) exists. |
Split("a,b", "") | An empty delimiter produces a single array element containing the original text. |
Split("a,,b", ",") | Adjacent delimiters create an empty middle element. |
Split("a,b", ";") | When the delimiter is absent, the result has one element containing the whole string. |
myArray[0] | Incorrect VBScript indexing; use myArray(0). |
| Unexpected spaces in the output | Apply Trim() to individual elements, or use a more specific delimiter. |
| HTML tags appear in submitted values | Use Server.HTMLEncode() for HTML text output and validate input before storing or processing it. |
| Quoted commas inside a CSV field | Use a CSV-aware parser; Split() treats every matching delimiter literally. |
It returns a one-dimensional, zero-based VBScript array. Access items with indexes such as parts(0) or loop with For Each.
A single space. For comma-separated values, supply the delimiter explicitly: Split(text, ",").
Use Split(text, ",", 2). The second array element contains any remaining comma-separated content.
No. Split("A, B", ",") gives "A" and " B". Use Trim() on each item when necessary.
Pass vbTextCompare as the fourth argument, for example Split(text, "x", -1, vbTextCompare). This changes delimiter comparison, not the case of the returned text.
Not reliably. CSV fields can contain quoted commas, doubled quotes and line breaks. Use a CSV-aware implementation for those formats; Split() works best for simple delimited text.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| bobby | 23-04-2016 |
| Well, after split, then how to store that result to database? I mean in MS Access.. | ID | Name | |----|----------| | 1 | Welcome | | 2 | to | | 3 | plus2net, learn web programming and design| cause everytime i tried, it becomes comma separated values in one row | |