VBScript Split() in Classic ASP: Convert a String to an Array

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).

Split a comma-separated string

<%
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.

VBScript Split() syntax and arguments ↑

Split(expression[, delimiter[, count[, compare]]])

The expression is the string to divide. The remaining arguments are optional:

ArgumentPurposeExample
expressionText to split (required)"one,two"
delimiterText that marks each split; a single space is the default","
countMaximum number of parts; -1 returns all parts3
compareHow the delimiter is matched: vbBinaryCompare or vbTextComparevbTextCompare

For the compare argument, include the earlier optional arguments too; for example, use Split(text, "x", -1, vbTextCompare).

Split by spaces, commas, pipes and other delimiters ↑

Split a sentence using spaces

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.

Split a string with a pipe character

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.

Split on a multi-character separator

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.

Limit the number of parts returned ↑

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.

Access array elements and display all values ↑

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.

Case-sensitive and case-insensitive delimiters ↑

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.

Handle extra spaces and empty elements ↑

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.

Practical Classic ASP examples ↑

Get the filename from a script path

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.

Read a comma-separated list from a submitted form

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.

Combine the array values again using Join()

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.

Keep extra delimiter characters in the last value

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.

Split multi-line text

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.

Count the returned array items

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.

Special cases and common errors ↑

Input or issueWhat 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 outputApply Trim() to individual elements, or use a more specific delimiter.
HTML tags appear in submitted valuesUse Server.HTMLEncode() for HTML text output and validate input before storing or processing it.
Quoted commas inside a CSV fieldUse a CSV-aware parser; Split() treats every matching delimiter literally.

Frequently asked questions ↑

Does Split() return a string or an array?

It returns a one-dimensional, zero-based VBScript array. Access items with indexes such as parts(0) or loop with For Each.

What is the default separator in Split()?

A single space. For comma-separated values, supply the delimiter explicitly: Split(text, ",").

How do I get only the first two parts of a string?

Use Split(text, ",", 2). The second array element contains any remaining comma-separated content.

Does Split() automatically remove extra spaces?

No. Split("A, B", ",") gives "A" and " B". Use Trim() on each item when necessary.

How do I split case-insensitively?

Pass vbTextCompare as the fourth argument, for example Split(text, "x", -1, vbTextCompare). This changes delimiter comparison, not the case of the returned text.

Can Split() parse a full CSV file?

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.

Back to top ↑


ASP Home



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



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer