Left(), Right() and Mid() in Classic ASP

Use Left() to take characters from the beginning, Right() from the end, and Mid() from a chosen position of a string in Classic ASP.

Get the first, last and middle characters

<%
Dim string_var
string_var = "Welcome to plus2net.com"
Response.Write Left(string_var, 10) & "<br>"  ' Welcome to
Response.Write Right(string_var, 3) & "<br>"  ' com
Response.Write Mid(string_var, 12, 12)     ' plus2net.com
%>

Output:

Welcome to
com
plus2net.com
Important: VBScript uses 1-based character positions for Mid(). The first character is position 1, not 0. You can copy the examples into a .asp file and run them on a Classic ASP-enabled IIS server.

Syntax and parameters

Left(string, length)
Right(string, length)
Mid(string, start[, length])
FunctionWhat it returnsParameters
Left()Characters from the beginningstring and required length
Right()Characters from the endstring and required length
Mid()Characters from a starting positionstring, required start, optional length

The length parameter counts characters, not the ending character position. For example, Mid("abcdef", 2, 3) returns bcd (positions 2, 3 and 4). Refer to Len() when you need to find a string's length first.

Right(string_var, number) returns the specified number of characters from the right-hand end. The original example extracts com from a domain-like string.

<%
Dim string_var, string_part
string_var = "Welcome to plus2net.com"
string_part = Right(string_var, 3)
Response.Write string_part
%>

Output: com.

For a short string, requesting more characters than it contains returns the entire string; requesting zero characters returns an empty string.

<%
Response.Write Right("ASP", 2) & "<br>"  ' SP
Response.Write Right("ASP", 10) & "<br>" ' ASP
Response.Write Len(Right("ASP", 0))    ' 0
%>

Left(): take characters from the beginning

Left(string_var, number) returns the specified number of characters from the beginning. It is useful for creating a short preview, reading a known prefix, or checking the first characters.

<%
Dim string_var, string_part
string_var = "Welcome to plus2net.com"
string_part = Left(string_var, 10)
Response.Write string_part
%>

Output: Welcome to.

<%
Dim textValue
textValue = "abcdef"
Response.Write Left(textValue, 3) & "<br>" ' abc
Response.Write Left(textValue, 0) & "<br>" ' empty string
Response.Write Left(textValue, 9)          ' abcdef
%>

Mid(): extract characters from any position

Mid(string_var, starting_from, length) starts at a 1-based character position. The third argument is optional; without it, the function returns text from that position through the end.

<%
Dim string_var, string_part
string_var = "Welcome to plus2net.com"
string_part = Mid(string_var, 12, 12)
Response.Write string_part
%>

Output: plus2net.com.

<%
Dim value
value = "abcdef"
Response.Write Mid(value, 2, 3) & "<br>" ' bcd
Response.Write Mid(value, 4) & "<br>"    ' def
Response.Write Mid(value, 20, 3)          ' empty string
%>

Why does Mid() start at 1?

Unlike indexes in some other programming languages, VBScript string positions start with the number 1. In "abcdef", a is position 1 and b is position 2. If start is beyond the end of the string, Mid() returns an empty string.

Extract a part of a URL

The existing URL example uses a known string and position to extract the hostname.

<%
Dim string_var, string_part
string_var = "https://www.google.com"
string_part = Mid(string_var, 9, 14)
Response.Write string_part
%>

Output: www.google.com.

Character position: The prefix https:// has eight characters, so www begins at position 9. Starting at position 8 would include the last slash.

Find the hostname without a fixed character position

For an illustrative URL containing ://, find its end with InStr(), then take the text up to the next slash. This avoids hard-coding position 9 for every scheme.

<%
Dim urlText, markerPos, hostStart, remainder, slashPos, hostName
urlText = "https://www.google.com/tutorial"
markerPos = InStr(urlText, "://")
If markerPos > 0 Then
    hostStart = markerPos + 3
    remainder = Mid(urlText, hostStart)
    slashPos = InStr(remainder, "/")
    If slashPos > 0 Then
        hostName = Left(remainder, slashPos - 1)
    Else
        hostName = remainder
    End If
    Response.Write Server.HTMLEncode(hostName)
End If
%>

Output: www.google.com. This is a simple string-processing example, not a full URL parser. Production code must handle URL edge cases such as query strings, fragments, ports and unusual authorities.

Practical uses of Left(), Right() and Mid()

Extract a file extension after the last dot

InStrRev() finds the last occurrence of a character. Use Mid() to return the part after that dot, while checking that the filename actually has an extension.

<%
Dim fileName, dotPos, extension
fileName = "report.final.pdf"
dotPos = InStrRev(fileName, ".")
If dotPos > 0 And dotPos < Len(fileName) Then
    extension = Mid(fileName, dotPos + 1)
    Response.Write Server.HTMLEncode(extension)
Else
    Response.Write "No extension found"
End If
%>

Output: pdf. For report or report., the example displays No extension found.

Extract an ID after the last separator

For text stored in a known format such as order-2026-157, locate the final hyphen and use Mid() to read the remaining identifier.

<%
Dim orderCode, hyphenPos
orderCode = "order-2026-157"
hyphenPos = InStrRev(orderCode, "-")
If hyphenPos > 0 Then
    Response.Write Mid(orderCode, hyphenPos + 1)
End If
%>

Output: 157.

Preview a long string

You can show the first 25 characters of a longer string and add an ellipsis only when the text exceeds that limit.

<%
Dim message, preview
message = "Learn Classic ASP string functions with working examples"
If Len(message) > 25 Then
    preview = Left(message, 25) & "..."
Else
    preview = message
End If
Response.Write Server.HTMLEncode(preview)
%>

Output: Learn Classic ASP string .... An ellipsis is added to the displayed preview, not to the original value.

Get the last four characters

Right() can display a short suffix when a source value is already suitable for display.

<%
Dim referenceCode
referenceCode = "INV-2026-4821"
Response.Write Right(referenceCode, 4)
%>

Output: 4821.

Read a submitted form field safely

Check a submitted value's length on the server before extracting a preview. HTML-encode the result when displaying visitor-provided text. See Classic ASP form processing for the full GET/POST workflow.

<%
Dim userMessage, displayText
userMessage = Trim(Request.Form("message"))
If Len(userMessage) > 0 Then
    displayText = Left(userMessage, 40)
    Response.Write Server.HTMLEncode(displayText)
Else
    Response.Write "Please enter a message."
End If
%>

Empty strings, Null values and boundary conditions

ExpressionResultReason
Left("abcdef", 2)abFirst two characters
Right("abcdef", 2)efLast two characters
Mid("abcdef", 2, 3)bcdThree characters starting at position 2
Mid("abcdef", 4)defAll characters from position 4
Left("abcdef", 0)Empty stringZero requested characters
Mid("abcdef", 20, 2)Empty stringStart is beyond the final character

A negative length is invalid. Mid() requires a positive start. When a source may be Null (for example, a database field), check it before treating the value as a normal string.

<%
Dim possiblyNull
possiblyNull = Null
If IsNull(possiblyNull) Then
    Response.Write "No text available"
Else
    Response.Write Server.HTMLEncode(Left(possiblyNull, 5))
End If
%>

Common errors and frequently asked questions

Why does Mid(text, 0, 3) fail?

Mid() expects positions beginning with 1. Use Mid(text, 1, 3) to extract the first three characters.

What happens when the length is larger than the source string?

Left() and Right() return the available characters. Mid() returns the available characters from the specified start position, provided the start is valid.

Can Mid() return everything after a position?

Yes. Omit the third argument: Mid("abcdef", 4) returns def.

Should I use character positions to parse every website URL?

No. Fixed positions are suitable only for predictable text formats. Real URLs have different schemes, hosts and components; use appropriate URL parsing and validation for production applications.

Can these functions process form input?

Yes, but first validate the submitted data and HTML-encode output. Using Left() for a displayed preview does not make arbitrary input safe by itself.

Does Right() count from position zero?

No. Right() accepts a count of characters to take from the end, not a numeric starting index. Mid() is the function that accepts a starting position.

How can I find the right starting position automatically?

Use InStr() or InStrRev() to find a delimiter, then pass the matching position to Mid().

Why is the copied code different from the visual syntax coloring?

The colour is only visual markup. Copying the code block preserves the underlying Classic ASP text, including quotes and comparison operators.

Continue with string searching, length, conversion and array handling:

Back to top ↑


ASP Home






✖
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