Use Left() to take characters from the beginning, Right() from the end, and Mid() from a chosen position of a string in Classic ASP.
<%
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.comMid(). 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.Left(string, length)
Right(string, length)
Mid(string, start[, length])| Function | What it returns | Parameters |
|---|---|---|
Left() | Characters from the beginning | string and required length |
Right() | Characters from the end | string and required length |
Mid() | Characters from a starting position | string, 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(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(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
%>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.
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.
https:// has eight characters, so www begins at position 9. Starting at position 8 would include the last slash.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.
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.
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.
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.
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.
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
%>| Expression | Result | Reason |
|---|---|---|
Left("abcdef", 2) | ab | First two characters |
Right("abcdef", 2) | ef | Last two characters |
Mid("abcdef", 2, 3) | bcd | Three characters starting at position 2 |
Mid("abcdef", 4) | def | All characters from position 4 |
Left("abcdef", 0) | Empty string | Zero requested characters |
Mid("abcdef", 20, 2) | Empty string | Start 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
%>Mid() expects positions beginning with 1. Use Mid(text, 1, 3) to extract the first three characters.
Left() and Right() return the available characters. Mid() returns the available characters from the specified start position, provided the start is valid.
Yes. Omit the third argument: Mid("abcdef", 4) returns def.
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.
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.
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.
Use InStr() or InStrRev() to find a delimiter, then pass the matching position to Mid().
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:
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.